title: Configuring Log4j2 in web application using log4j-web.jar
body: <p>I am trying to configure log4j2 in my webapplication by following some tutorials. I am working with glassfish 4.1.1 server and servlet version 3.1. I am able to configure the logging feature with the below configuration:</p>
  
  <p>log4j.properties</p>
  
  <pre><code>     # Root logger option
       log4j.rootLogger=INFO, consoleAppender, fileAppender
  
       # debug level logger
       log4j.logger.kumar.suraj.college.administration.login=DEBUG
  
       # Redirect log messages to console
       log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
       log4j.appender.consoleAppender.Target=System.out
       log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
       log4j.appender.consoleAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  
       # Redirect log messages to a log file, support file rolling.
       log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender
       log4j.appender.fileAppender.File=E:\\DEVELOPMENT\\JAVA\\web-logs\\web-college-administration\\applicationLogs.log
       log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
       log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  </code></pre>
  
  <p>the properties file is placed in src/main/resources folder</p>
  
  <p>web.xml</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;
          &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"   version="3.1"&gt;
         &lt;display-name&gt;College Administration&lt;/display-name&gt;
         &lt;!--     &lt;context-param&gt;
                      &lt;param-name&gt;log4jConfiguration&lt;/param-name&gt;
                      &lt;param-value&gt;/log4j.properties&lt;/param-value&gt;
                  &lt;/context-param&gt;is it required
           --&gt;
         &lt;!--from where is this class referenced in dependency without web --&gt;
         &lt;listener&gt;
              &lt;listener-class&gt;org.apache.logging.log4j.web.Log4jServletContextListener&lt;/listener-class&gt;
         &lt;/listener&gt;
        &lt;servlet&gt;
            &lt;servlet-name&gt;loginServlet&lt;/servlet-name&gt;
            &lt;servlet-class&gt;kumar.suraj.college.administration.login.LoginServlet&lt;/servlet-class&gt;
        &lt;/servlet&gt;
        &lt;servlet-mapping&gt;
            &lt;servlet-name&gt;loginServlet&lt;/servlet-name&gt;
            &lt;url-pattern&gt;/login&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
  
        &lt;/web-app&gt;
  </code></pre>
  
  <p>the context param that defines file location is commented out. probably log4j looks by default for a file with name log4j.properties. However just wanted to know if its the right way to specify the file location.</p>
  
  <p>Also I am not sure from which jar is org.apache.logging.log4j.web.Log4jServletContextListener being referenced. I searched all jar files but could not locate this class.</p>
  
  <p>LoginServlet.java</p>
  
  <pre><code>   package kumar.suraj.college.administration.login;
  
     import java.io.IOException;
     import javax.servlet.ServletException;
     import javax.servlet.http.HttpServlet;
     import javax.servlet.http.HttpServletRequest;
     import javax.servlet.http.HttpServletResponse;
     import org.apache.log4j.Logger;
  
     import kumar.suraj.college.administration.adduser.AddUserServlet;
  
     public class LoginServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
  
          // final static Logger logger = LogManager.getLogger(LoginServlet.class);
          final static Logger logger = Logger.getLogger(LoginServlet.class);
  
          public LoginServlet() {
             super();
          }
  
          @Override
          protected void doGet(final HttpServletRequest request, final     HttpServletResponse response)
          throws ServletException, IOException {
             LoginServlet.logger.debug("debug level logging supported");              response.getWriter().append("Servedat:").append(request.getContextPath());
      response.getWriter().append("Hello Suraj");
  }
  
         @Override
         protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
                 LoginServlet.logger.debug("debug level logging supported");
                 this.doGet(request, response);
         }
    }
  </code></pre>
  
  <p>pom.xml dependency for log4j</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;log4j&lt;/groupId&gt;
          &lt;artifactId&gt;log4j&lt;/artifactId&gt;
          &lt;version&gt;1.2.17&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>All this works fine and I get logs in both console as well as file. But when I try to change the configuration as per the following links :</p>
  
  <p><a href="https://logging.apache.org/log4j/2.x/manual/webapp.html#Servlet-3.0" rel="nofollow">https://logging.apache.org/log4j/2.x/manual/webapp.html#Servlet-3.0</a>
  <a href="https://logging.apache.org/log4j/2.x/maven-artifacts.html" rel="nofollow">https://logging.apache.org/log4j/2.x/maven-artifacts.html</a></p>
  
  <p>Like instead of dependency specified earlier I switch to </p>
  
  <pre><code>        &lt;dependency&gt;
             &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
             &lt;artifactId&gt;log4j-web&lt;/artifactId&gt;
             &lt;version&gt;2.6.2&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>log4j-api and log4j-core are added as transitive dependencies with log4j-web.jar</p>
  
  <p>other changes I made in LoginServlet.java is because of the compile time error I was getting after switching to log4j-web.jar which is as below :</p>
  
  <pre><code>     import org.apache.logging.log4j.LogManager;
       import org.apache.logging.log4j.Logger;
  
  
       public class LoginServlet extends HttpServlet {
           private static final long serialVersionUID = 1L;
           final static Logger logger = LogManager.getLogger(LoginServlet.class);
           // final static Logger logger = Logger.getLogger(LoginServlet.class);
  </code></pre>
  
  <p>the main changes are in the initialization of logger variable and the two imports. rest all configuration remained as it is. Also i was able to locate the listener class specified in web.xml in log4j-web.jar in this case. Still logging is not working with this configuartion.</p>
  
  <p>Could some one please help me with it or tell me what I am doing wrong here ?</p>