我正在使用log4j2通过lombok插件进行日志记录。但是,我的应用程序似乎没有使用log4j2进行日志记录,并且总是退回commons-logging。我尝试了多种选择,但似乎没有任何效果。任何帮助将不胜感激。
我排除了Maven依赖项中的commons-logging,因此代码选择了log4j2,但是却出现了java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory。
Maven依赖项
<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属性文件
<?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>
控制器代码
@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;
}
}
答案 0 :(得分:0)
我已经克隆了您的项目并看到了问题。不要排除commons-logging
。将log4j.xml
移动到src/main/resources
,然后(可选)在src/main/resources/log4j.xml
中将日志路径更改为fileName="../logs/springMvcTest.log"
。以下是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
让Spring框架使用commons-logging
。对于您的应用特定日志记录,请继续使用log4j
。