'Log4j2 - Program doesn't log when replacing Log4j2 imports by slf4j ones

My programs stops log in files just by replacing:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

with

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

I'm using : OracleJdk8, log4j-core-2.17.1 and slf4j-sample-2.0.0-alpha6

Please find bellow my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log-path">C:/path/to/logfile</Property>
    </Properties>
    <Appenders>
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
        <RollingFile name="functional-log" fileName="${log-path}/functional.log" filePattern="${log-path}/functional-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY" />
        </RollingFile>
        <RollingFile name="technical-1-log" fileName="${log-path}/technical.log" filePattern="${log-path}/technical-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <LevelRangeFilter minLevel="DEBUG" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
        </RollingFile>
        <RollingFile name="technical-2-log" fileName="${log-path}/technical.log" filePattern="${log-path}/technical-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
            <LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="ACCEPT" onMismatch="DENY" />
        </RollingFile>
        <RollingFile name="error-log" fileName="${log-path}/error.log" filePattern="${log-path}/error-%d{yyyy-MM-dd}.err">
            <PatternLayout>
                <pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="x.y.z" level="ALL" additivity="false">
            <appender-ref ref="console-log" level="ALL" />
            <appender-ref ref="functional-log" level="INFO" />
            <appender-ref ref="technical-1-log" level="TRACE" />
            <appender-ref ref="technical-2-log" level="WARN" />
            <appender-ref ref="error-log" level="ERROR" />
        </Logger>
        <Root level="TRACE" additivity="false">
            <AppenderRef ref="console-log" />
        </Root>
    </Loggers>
</Configuration>

By this configuration, I'd like to log :

  • "ERROR" and "FATAL" in error.err file.
  • "TRACE", "DEBUG" and "WARN" in technical.log file.
  • "INFO" in functional.log file.

Could you please indicate me the right way to use slf4j import to still logging in RollingFile?

Knowing that org.slf4j.Logger.fatal() method doesn't exist :(

Thank you so much for you help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source