web-dev-qa-db-ja.com

log4j2に書き込まれていないSpringログ

Springとlog4jは初めてです。Springフレームワークを使用し、log4j2ライブラリを使用してサンプルのHelloWorldプロジェクトを試しています。 srcフォルダーにlog4j2.xmlがあります。アプリケーションを実行すると、アプリケーションログのみがログファイルに書き込まれます。春のログは書き込まれていませんが、コンソールで確認できます。クラスパスにcommonslogging jar(Spring依存関係)、log4j2、およびSpringjarがあります。ここで構成が不足している場合、誰かが私を助けてくれますか?

私のlog4j2xmlファイル、

<?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

スプリングログが出力ファイルにありません。

ありがとう、スマ

13

他の回答は実際の回答では詳しく説明されていないため、解決策は、以下のCommons LoggingBridgeの依存関係をMavenpom.xmlファイルに追加することです。

Apache Log4j Webページ に記載されているように:

既存のコンポーネントがApacheCommons Logging 1.xを使用していて、このロギングをLog4j 2にルーティングする場合は、以下を追加しますが、Commons Logging1.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
28
conorgriffin