gpt4 book ai didi

java - Spring mcvc 应用程序中未选取 Log4j2 属性

转载 作者:行者123 更新时间:2023-12-02 02:30:39 26 4
gpt4 key购买 nike

我正在使用 log4j2 通过 lombok 插件进行日志记录。然而,我的应用程序似乎没有使用 log4j2 进行日志记录,并且总是回退到公共(public)日志记录。我尝试了多种选择,但似乎没有任何效果。非常感谢任何帮助。

我在 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>

Controller 代码

@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/ap‌​ache/logging/log4j/Logger.class"));
//core
System.out.println(org.apache.logging.log4j.Logger.class.getResource("/org/ap‌​ache/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;
}
}

最佳答案

我已经克隆了您的项目并看到了该问题。不要排除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

关于java - Spring mcvc 应用程序中未选取 Log4j2 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com