title: Initialize slf4j with log4j2.xml
body: <p>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):</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
         &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
         &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
         &lt;version&gt;${log4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
         &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
         &lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
         &lt;version&gt;${log4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
         &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
         &lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
         &lt;version&gt;${log4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt; 
  </code></pre>
  
  <p>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</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I searched for a proper way to provide the configuration file and ended up with this:</p>
  
  <pre><code>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());
  </code></pre>
  
  <p>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.</p>
  
  <p>Is there any way to provide the configuration file (log4j2.xml) to slf4j in order to see all the appenders and loggers?</p>
  
  <p><strong>Update</strong> 
  Consider this my configuration file (I replaced the original packages and appenders name):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration&gt;
      &lt;Properties&gt;
          &lt;Property name="def.files.backup.count"&gt;10&lt;/Property&gt;
          &lt;Property name="log.file.path"&gt;${oo.home}/var/logs&lt;/Property&gt;
          &lt;Property name="def.file.max.size"&gt;10MB&lt;/Property&gt;
          &lt;Property name="log.level"&gt;WARN&lt;/Property&gt;
      &lt;/Properties&gt;
  
      &lt;ThresholdFilter/&gt;
  
      &lt;Appenders&gt;
          &lt;RollingFile name="Appender1" fileName="${log.file.path}/file1.log" maxFileSize="${def.file.max.size}"
                       maxBackupIndex="${def.files.backup.count}"&gt;
              &lt;PatternLayout&gt;org.apache.log4j.PatternLayout&lt;/PatternLayout&gt;
              &lt;Policies&gt;
                  &lt;SizeBasedTriggeringPolicy size="10MB"/&gt;
              &lt;/Policies&gt;
          &lt;/RollingFile&gt;
  
          &lt;RollingFile name="Appender2" fileName="${log.file.path}/file2.log"
                       maxFileSize="${def.file.max.size}"
                       maxBackupIndex="${def.files.backup.count}"&gt;
              &lt;PatternLayout&gt;org.apache.log4j.PatternLayout&lt;/PatternLayout&gt;
              &lt;Policies&gt;
                  &lt;SizeBasedTriggeringPolicy size="10MB"/&gt;
              &lt;/Policies&gt;
          &lt;/RollingFile&gt;
  
          &lt;RollingFile name="Appender3" fileName="${log.file.path}/file3.log"
                       maxFileSize="${def.file.max.size}"
                       maxBackupIndex="${def.files.backup.count}"&gt;
              &lt;PatternLayout&gt;org.apache.log4j.PatternLayout&lt;/PatternLayout&gt;
              &lt;Policies&gt;
                  &lt;SizeBasedTriggeringPolicy size="10MB"/&gt;
              &lt;/Policies&gt;
          &lt;/RollingFile&gt;
  
      &lt;/Appenders&gt;
  
      &lt;Loggers&gt;
          &lt;AsyncLogger name="com.package1.oo" level="${log.level}" additivity="false"&gt;
              &lt;AppenderRef ref="Appender1"/&gt;
          &lt;/AsyncLogger&gt;
  
          &lt;AsyncLogger name="io.package2" level="${log.level}" additivity="false"&gt;
              &lt;AppenderRef ref="Appender2"/&gt;
          &lt;/AsyncLogger&gt;
  
          &lt;AsyncLogger name="com.package3.package3" level="${log.level}" additivity="false"&gt;
              &lt;AppenderRef ref="Appender3"/&gt;
          &lt;/AsyncLogger&gt;
  
          &lt;AsyncLogger name="org.package4" level="${log.level}"&gt;
  
          &lt;/AsyncLogger&gt;
  
          &lt;AsyncLogger name="com.package5.Class1" level="${log.level}"&gt;
  
          &lt;/AsyncLogger&gt;
  
          &lt;AsyncRoot level="${log.level}"&gt;
              &lt;AppenderRef ref="Appender1"/&gt;
              &lt;AppenderRef ref="Appender2"/&gt;
              &lt;AppenderRef ref="Appender3"/&gt;
  
          &lt;/AsyncRoot&gt;
      &lt;/Loggers&gt;
  
  &lt;/Configuration&gt;
  </code></pre>