未在 log4j2 中写入的弹簧日志

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

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

输出文件中缺少弹簧日志。

谢谢 苏玛


答案 1

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

Apache Log4j网页所述:

如果现有组件使用Apache Commons Logging 1.x,并且您希望将此日志记录路由到Log4j 2,请添加以下内容,但不要删除任何Commons Logging 1.x依赖项。

<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

答案 2

推荐