gpt4 book ai didi

java - Spring 日志不是用 log4j2 写的

转载 作者:IT老高 更新时间:2023-10-28 13:58:29 24 4
gpt4 key购买 nike

我是 spring 和 log4j 的新手。我正在尝试一个带有 spring 框架并使用 log4j2 库的示例 Hello World 项目。我的 src 文件夹中有 log4j2.xml。当我运行应用程序时,只有我的应用程序日志写入日志文件。没有写 Spring 日志。但是我可以在控制台中看到它们。我的类路径中有公共(public)日志记录 jar(spring 依赖项)、log4j2 和 spring jar。如果我在这里缺少任何配置,谁能帮助我?

我的 log4j2 xml 文件,

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" monitorInterval="5">
<Appenders>
<Console name="consoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<File name="fileAppender" fileName="learning.log" append="true">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
</Appenders>

<Loggers>
<Root level="trace">
<AppenderRef ref="consoleAppender"/>
<AppenderRef ref="fileAppender"/>
</Root>
</Loggers>
</configuration>

我的代码:

public class MainApp {
static Logger log = LogManager.getLogger(MainApp.class.getName());

public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");

log.info("Going to create HelloWord Obj");

HellowWorld obj = (HellowWorld) context.getBean("helloWorld");

obj.getMessage();

log.info("Exiting the program");
}
}

输出:

main INFO  springExample.MainApp - Going to create HelloWord Obj
main INFO springExample.MainApp - Exiting the program

输出文件中缺少 spring 日志。

谢谢,苏马

最佳答案

由于其他答案在实际答案中没有说明,因此解决方案是将下面的 Commons Logging Bridge 依赖项添加到 Maven pom.xml 文件中。

Apache Log4j web page 中所述:

If existing components use Apache Commons Logging 1.x and you want to have this logging routed to Log4j 2, then add the following but do not remove any Commons Logging 1.x dependencies.

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

在下面添加这个依赖的效果见下例:

之前:

May 11, 2015 8:10:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Mon May 11 20:10:41 IST 2015]; root of context hierarchy
May 11, 2015 8:10:42 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [datapower.xml]
May 11, 2015 8:10:42 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
May 11, 2015 8:10:42 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromContextPath
INFO: Creating JAXBContext with context path [com.datapower.schemas.management]
May 11, 2015 8:10:45 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromContextPath
INFO: Creating JAXBContext with context path [com.datapower.schemas.management]
May 11, 2015 8:10:47 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol

之后:

22:07:21.925 [main] INFO  org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@22eeefeb: startup date [Mon May 11 22:07:21 IST 2015]; root of context hierarchy
22:07:21.950 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [datapower.xml]
22:07:22.059 [main] INFO org.springframework.ws.soap.saaj.SaajSoapMessageFactory - Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
22:07:22.068 [main] INFO org.springframework.oxm.jaxb.Jaxb2Marshaller - Creating JAXBContext with context path [com.datapower.schemas.management]
22:07:24.446 [main] INFO org.springframework.oxm.jaxb.Jaxb2Marshaller - Creating JAXBContext with context path [com.datapower.schemas.management]
22:07:26.554 [main] INFO org.springframework.ws.soap.saaj.SaajSoapMessageFactory - Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol

关于java - Spring 日志不是用 log4j2 写的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28934834/

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