I am using log4j2 for logging via lombok plugin. However my application does not seem to be using log4j2 for logging and always falls back to commons-logging. I tried multiple options, but nothing seem to be working. Any help is greatly appreciated.
I excluded commons-logging in maven dependencies so the code picks up log4j2, but I get java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory.
Maven dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1.1</version>
</dependency>
Log4j XML properties file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="springMvcTest.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
Controller code
@Log4j2
@Controller
public class HelloWorldController {
@RequestMapping(value = "/helloJson", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public RequestData helloJson(HttpEntity<String> entity, HttpServletRequest inpreq, HttpServletResponse inpres, @RequestParam Map<String, String> params){
//api
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/apache/logging/log4j/Logger.class"));
//core
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/apache/logging/log4j/core/Appender.class"));
//config
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/log4j2.xml"));
log.info("Input => {} / {} % {}",inpreq.getMethod(),inpreq.getServletPath(), params);
return params;
}
}
I have cloned your project and saw the issue. Do not exclude commons-logging
. Move you log4j.xml
to src/main/resources
and (optionally) in src/main/resources/log4j.xml
change the log path to fileName="../logs/springMvcTest.log"
. Here are the log statements from springMvcTest.log
:
2019-07-28 21:14:57.516 [http-nio-84-exec-30] INFO com.test.epi.controller.HelloWorldController - Input => GET / /helloJson % {}
2019-07-28 21:14:57.518 [http-nio-84-exec-30] INFO com.test.epi.controller.HelloWorldController - Logger class: class org.apache.logging.log4j.core.Logger
Let the Spring framework use commons-logging
. For your app-specific logging continue to use log4j
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.