title: Log4j2 WebLookup
body: <p>I'm using log4j2 (2.0.2) in a servlet 3.0 web application, running in Tomcat 8.0.12. I'm trying to use the weblookup to get the web app context path to use as part of the log file name. (We run the same war file under several different contexts using properties to configure it).</p>
  
  <p>The log4j2.xml file includes</p>
  
  <pre><code>        &lt;RollingFile name="Web" fileName="/srv/dnsnet/logs/web-${web:contextPath}.logfile" filePattern="/srv/dnsnet/logs/web-${web:contextPath}-%d{yyyy-MM-dd}-%i.log.gz" append="true" immediateFlush="false"&gt;
          &lt;PatternLayout&gt;
              &lt;Pattern&gt;%d %-5p [%25.25t] %40.40c{3.} %15X{sysid} %m%n&lt;/Pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;TimeBasedTriggeringPolicy /&gt;
          &lt;/Policies&gt;
          &lt;DefaultRolloverStrategy max="8"/&gt;
      &lt;/RollingFile&gt;
  </code></pre>
  
  <p>But the web:contextPath does not get resolved. I have got log4j-web-2.0.2.jar in my web app. I added some messages to the WebLookup class, and the problem is that the external context of the LoggerContext is null.</p>
  
  <p>So I tried to see where the LoggerContext is created. The stack trace when the LoggerContext constructor is called is </p>
  
  <pre><code>at org.apache.logging.log4j.core.LoggerContext.&lt;init&gt;(LoggerContext.java:114)
      at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
      at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:113)
      at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:82)
      at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
      at org.apache.logging.log4j.LogManager.getContext(LogManager.java:269)
      at org.apache.logging.log4j.jcl.LogFactoryImpl$PrivateManager.getContext(LogFactoryImpl.java:108)
      at org.apache.logging.log4j.jcl.LogFactoryImpl.getLoggersMap(LogFactoryImpl.java:52)
      at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:43)
      at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:75)
      at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
      at org.apache.myfaces.webapp.AbstractMyFacesListener.&lt;clinit&gt;(AbstractMyFacesListener.java:34)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
      at java.lang.Class.newInstance(Class.java:433)
      at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151)
      at org.apache.catalina.core.ApplicationContext.addListener(ApplicationContext.java:1231)
      at org.apache.catalina.core.ApplicationContextFacade.addListener(ApplicationContextFacade.java:647)
      at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:110)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5164)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
      at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:581)
      at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1686)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745
  </code></pre>
  
  <p>But the ClassLoaderContextSelector always passes null for the external context. So it looks as though I'm missing something.</p>
  
  <p>Can anyone suggest what I'm doing wrong, or missing?  Do I have to specify a different ContextSelector?</p>