I am new to spring and log4j.I am trying a sample Hello World project with spring framework and using log4j2 library. I have log4j2.xml in my src folder. When i run the application, only my application logs are written in the log file. The spring logs are not written.However i can see them in the console. I have commons logging jar (spring dependency), log4j2 and spring jars in my classpath. Can anyone help me if I am missing any configuration here?
My log4j2 xml file,
<?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>
My Code:
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");
}
}
output:
main INFO springExample.MainApp - Going to create HelloWord Obj
main INFO springExample.MainApp - Exiting the program
The spring logs are missing in the output file.
Thanks, Suma
In a Spring Boot application, the log4j2.properties file will typically be in the resources folder. Before we start configuring Log4J 2, we will write a Java class to generate log messages via Log4J 2.
Using Spring Boot you can specify log4j2.properties inside the application.properties Show activity on this post. it also seems to be necessary to set log4j as standard apache logging. In general it uses logback. So I had to add Show activity on this post. Change the POM.xml file as below and it should be working.
Like any other Java properties file, a log4j2.properties file are a set of key value pairs with options to configure the various components of Log4J 2, such as loggers, appenders, and layouts. A basic log4j2.properties file starts with a name, optional properties to be used in other parts of the file, and appender declarations. . . .
Syslog Appender Appenders are the main component in logging frameworks that deliver logging data to a destination. Log4j2 supports many appenders, such as the Syslog appender. Let's update our log4j2-spring.xml file to add the Syslog appender for sending the logging data to the Syslog server: The Syslog appender has many attributes: 3.
Since the other answers don't spell it out in the actual answer, the solution is to add the Commons Logging Bridge dependency below to the Maven pom.xml file.
As stated on the 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>
See the example below of the effect of adding this dependency in:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With