簡體   English   中英

Spring日志沒有寫入log4j2

[英]Spring logs not written in log4j2

我是spring和log4j的新手。我正在嘗試使用spring框架和使用log4j2庫的示例Hello World項目。 我的src文件夾中有log4j2.xml。 當我運行應用程序時,只有我的應用程序日志寫在日志文件中。 彈簧日志沒有寫入。但是我可以在控制台中看到它們。 我在類路徑中有公共記錄jar(spring dependency),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網頁上所述

如果現有組件使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM