Logging Spring using Log4j2

hveiga picture hveiga · May 2, 2013 · Viewed 22.8k times · Source

I'm trying to use Log4j2 to print the spring logs into a file and console. I guess it is a problem in my Log4j2 configuration. I have not been able to get it working. I have this configuration in my log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration name="defaultConfiguration" status="warn" strict="true" monitorInterval="5">
    <properties>
        <property name="patternlayout">%d{ISO8601} [%t] %-5level %logger{36} - %msg%n%throwable{full}</property>
        <property name="filename">${env:MY_ROOT}/logs/mylog.log</property>
        <property name="filenamePattern">${env:MY_ROOT}/logs/mylog-%d{yyyy-dd-MM}-%i.log.gz</property>
    </properties>
    <appenders>
        <appender name="Console" type="Console" target="SYSTEM_OUT">
            <layout type="PatternLayout" pattern="${patternlayout}" />
        </appender>
        <appender name="File" type="RollingFile" fileName="${filename}" filePattern="${filenamePattern}" bufferedIO="true" immediateFlush="true"
        append="true">
            <layout type="PatternLayout" pattern="${patternlayout}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB" />
            </Policies>
            <DefaultRolloverStrategy max="30" />
        </appender>
        <appender name="AsynchFile" type="asynch" blocking="true" bufferSize="128">
            <appender-ref ref="File" />
        </appender>
    </appenders>
    <loggers>
        <root level="info">
            <appender-ref ref="Console" />
            <appender-ref ref="AsynchFile" />
        </root>
        <logger name="org.springframework.beans">
            <appender-ref ref="Console" />
            <appender-ref ref="AsynchFile" />
        </logger>
    </loggers>
</configuration>

These are the dependencies that I have in my pom file: (probably some of them are not required)

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.6</version>
</dependency>

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.6.6</version>
</dependency>

<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-api</artifactId>
 <version>2.0-beta5</version>
</dependency>

<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 <version>2.0-beta5</version>
</dependency>
<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-1.2-api</artifactId>
 <version>2.0-beta5</version>
</dependency>

<dependency>
 <groupId>com.lmax</groupId>
 <artifactId>disruptor</artifactId>
 <version>3.0.0.beta3</version>
</dependency>

I'm not doing anything related to spring in my Java code. I'm using the Main class from Apache Camel which reads my spring configuration and loads the beans.

What am I doing wrong? Thanks!

Edit: I am not getting spring logs in any output (console or file). However, I am able to get the logs I create in my java code. I hope this clarification will help.

Answer

Kouichi picture Kouichi · Feb 26, 2016

This issue caused by spring use common-logging 1.X ,so if you want to have this logging routed to Log4j 2,you need add dependencies in your pom.xml

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>2.1</version>
 </dependency>

do not remove your common-logging 1.X dependencies