Logging Spring using Log4j2
Solution 1
Looking at the dependencies in your pom, you have this one: slf4j-log4j12. This causes log statements against the slf4j api to be routed to the Log4j-1.2 implementation. You probably want these to be routed to the Log4j-2.0 implementation. Can you replace slf4j-log4j12 with log4j-slf4j-impl and try again?
Solution 2
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
hveiga
Updated on July 05, 2022Comments
-
hveiga almost 2 years
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.
-
hveiga about 11 yearsI have updated my post so you can have more info about my issue. Thank you for the help.
-
hveiga almost 11 yearsI didn't see you answer until today, I am sorry! I am going to test it and I will let you know. Thank you for the answer.
-
Remko Popma almost 10 yearsGlad to hear it. By the way, I recommend you upgrade to log4j-2.0-rc2 and lmax disruptor 3.2.1.
-
K Erlandsson about 8 yearsYou might want to add that the log4j-jcl version needs to match the log4j2 version used, or there will be class mismatch issues.
-
SpareTheRod about 5 yearsI had an even thornier issue where some other dependency was bringing in an older version of log4j-jcl without my knowledge, and that older version was wanting log4j1 instead of log4j2. Discovered this with dependency-tree. Something to watch out for.