I want to use slf4j over log4j. I added the following dependencies in my pom.xml (I used 1.7.25 for slf4j and 2.10.0 for log4j2):
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
Everything builds just fine, no compilation errors or lack of dependencies, but I failed to specify the configuration (log4j2.xml) file in my class that is responsible for the initialization of the Logger. In this situation it always prints the same warning
log4j:WARN No appenders could be found for logger (com.mypackage.etc).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I searched for a proper way to provide the configuration file and ended up with this:
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
context.setConfigLocation(file.toURI());
The problem is that in my case LogManager.getContext(false) will always return an instance of Slf4JLoggerContext (considering that I use slf4j as a facade for the logger) and the initialization will fail with ClassCastException. I tried to store that instance of Slf4JLoggerContext, but it doesn't offer a setter for the context. Also I failed to find a way to retrieve a LoggerContext from log4j.
Is there any way to provide the configuration file (log4j2.xml) to slf4j in order to see all the appenders and loggers?
Update Consider this my configuration file (I replaced the original packages and appenders name):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="def.files.backup.count">10</Property>
<Property name="log.file.path">${oo.home}/var/logs</Property>
<Property name="def.file.max.size">10MB</Property>
<Property name="log.level">WARN</Property>
</Properties>
<ThresholdFilter/>
<Appenders>
<RollingFile name="Appender1" fileName="${log.file.path}/file1.log" maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
<RollingFile name="Appender2" fileName="${log.file.path}/file2.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
<RollingFile name="Appender3" fileName="${log.file.path}/file3.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.package1.oo" level="${log.level}" additivity="false">
<AppenderRef ref="Appender1"/>
</AsyncLogger>
<AsyncLogger name="io.package2" level="${log.level}" additivity="false">
<AppenderRef ref="Appender2"/>
</AsyncLogger>
<AsyncLogger name="com.package3.package3" level="${log.level}" additivity="false">
<AppenderRef ref="Appender3"/>
</AsyncLogger>
<AsyncLogger name="org.package4" level="${log.level}">
</AsyncLogger>
<AsyncLogger name="com.package5.Class1" level="${log.level}">
</AsyncLogger>
<AsyncRoot level="${log.level}">
<AppenderRef ref="Appender1"/>
<AppenderRef ref="Appender2"/>
<AppenderRef ref="Appender3"/>
</AsyncRoot>
</Loggers>
</Configuration>