Подтвердить что ты не робот

Spring журналы, не записанные в log4j2

Я новичок в spring и log4j.Я пробую пример проекта Hello World с картой spring и используя библиотеку log4j2. У меня log4j2.xml в моей папке src. Когда я запускаю приложение, только журналы приложений записываются в файл журнала. Журналы spring не записываются. Однако я могу видеть их в консоли. У меня есть commons logging jar (spring dependency), log4j2 и spring jars в моем пути к классам. Может ли кто-нибудь помочь мне, если мне не хватает какой-либо конфигурации здесь?

Мой файл 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.

Спасибо, Suma

4b9b3361

Ответ 1

Поскольку другие ответы не отражают его в фактическом ответе, решение заключается в том, чтобы добавить зависимость моста Logons в Commons ниже к файлу 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[email protected]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[email protected]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