Search code examples
web-applicationslog4jlog4j2tomcat8java-ee-7

Configuring log4j2 in a web application


I'm not able to see debug logs I've enabled in the log4j2 configuration file.

My code organization is as below. I am using the gradle 'java' and 'war' plugin and my code organization is according to the defaults specified there.

.
├── main
│   ├── java
│   │   └── am
│   │       └── login
│   │           ├── common
│   │           │   └── LoginWebAppConfiguration.java
│   │           └── google
│   │               └── GoogleLogin.java
│   ├── resources
│   │   └── log4j2.xml
│   └── webapp
│       └── WEB-INF
│           ├── pages
│           │   └── google.html
│           ├── spring
│           │   └── mvc
│           │       └── login-servlet.xml
│           └── web.xml
└── test
    ├── java
    └── resources

My log4j2 configuration

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN" monitorInterval="5">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
            <File name="LogFile" fileName="logs/login.log">
                <PatternLayout>
                    <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
                </PatternLayout>
            </File>
    </Appenders>

    <Loggers>
        <Root level="DEBUG">
            <AppenderRef ref="Console" />
            <AppenderRef ref="LogFile"/>
        </Root>
        <Logger name="org.springframework" level="DEBUG" />
        <Logger name="am.login" level="DEBUG" />
    </Loggers>
</Configuration>

My web.xml doesn't explicitly configure log4j2 as I use Servlet specification 3.1 as stated here

This is my deployment structure

.
├── META-INF
│   ├── MANIFEST.MF
│   └── war-tracker
└── WEB-INF
    ├── classes
    │   ├── am
    │   │   └── login
    │   │       ├── common
    │   │       │   └── LoginWebAppConfiguration.class
    │   │       └── google
    │   │           └── Login.class
    │   └── log4j2.xml
    ├── lib
    │   ├── commons-logging-1.2.jar
    │   ├── log4j-api-2.9.0.jar
    │   ├── log4j-core-2.9.0.jar
    │   ├── log4j-slf4j-impl-2.9.0.jar
    │   ├── log4j-web-2.9.0.jar
    │   ├── slf4j-api-1.7.25.jar
    │   ├── spring-aop-4.3.11.RELEASE.jar
    │   ├── spring-beans-4.3.11.RELEASE.jar
    │   ├── spring-context-4.3.11.RELEASE.jar
    │   ├── spring-core-4.3.11.RELEASE.jar
    │   ├── spring-expression-4.3.11.RELEASE.jar
    │   ├── spring-web-4.3.11.RELEASE.jar
    │   └── spring-webmvc-4.3.11.RELEASE.jar
    ├── pages
    │   └── google.html
    ├── spring
    │   └── mvc
    │       └── login-servlet.xml
    └── web.xml

This is how I am importing log4j2 dependencies into my project

  // log using slf4j
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'

    // reroute slf4j to log4j2
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.9.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.0'

    // In order to properly support and handle the ClassLoader environment and container lifecycle of a JEE web application
    compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.9.0'

What am I missing here?


Solution

  • Spring uses JCL for logging. I needed to include the log4j2 implementation of JCL API in my classpath for the spring logs to appear. Refer to this

    My build.gradle now looks like this for logging dependencies

        // log using slf4j
        compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    
        // log4j2 binding or implementation for slf4j
        compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.9.0'
    
        // spring uses apache commons logging api. This provides a log4j2 implementation for commons logging
        compile 'org.apache.logging.log4j:log4j-jcl:2.9.0'
    

    My question seems to suggest that none of the logging was appearing including my application logs. I was wrong there. My sample application didn't have any logs yet and I mistook the non-appearance of spring debug logs.