|
Following example shows how to use Log4J 1.x in spring core.
Example
Log4j dependency
To use Log4j with the JCL API (the default spring logging dependency), we need to add Log4j dependency:
pom.xml<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties
We also have to provide log4j.properties in classpath.
src/main/resources/log4j.propertieslog4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n
Different log formatting options are provided here.
Example Bean
package com.logicbig.example;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyBean {
private static Log log = LogFactory.getLog(MyBean.class);
public void doSomething() {
log.info("doing something");
}
}
Main class
package com.logicbig.example;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.Collections;
@Configuration
public class ExampleMain {
@Bean
public MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) {
ConfigurableApplicationContext context =
new AnnotationConfigApplicationContext(ExampleMain.class);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}
Output25-11-10 21:36:47:638 INFO com.logicbig.example.ExampleMain.main() example.MyBean:10 - doing something
Directly Using Log4j API
Instead of using JCL API, we can use Log4j API directly.
Just modify the bean class to use Log4j API directly, the rest of the classes will remain same.
package com.logicbig.example;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class MyBean {
private static final Logger log = LogManager.getLogger(MyBean.class);
public void doSomething() {
log.info("doing something");
}
}
The output will be same. In this case since our code is directly invoking log4j API, JCL will not be involved at all, but Spring internal logging will still go through JCL layer as they are still using JCL API. See the complete example here
End of support for log4j 1.x
Starting Spring 5, the new module spring-jcl was introduced which doesn't implicitly support log4j 1.x. Log4j 1.x has reached it's end of life already. Above example will not work if we use Spring 5 or above. It is recommended to use Log4j 2 (next tutorial).
Example ProjectDependencies and Technologies Used: - spring-context 4.3.8.RELEASE (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 4.3.30.RELEASE Version compatibilities of spring-context with this example:
- 3.2.3.RELEASE
- 3.2.4.RELEASE
- 3.2.5.RELEASE
- 3.2.6.RELEASE
- 3.2.7.RELEASE
- 3.2.8.RELEASE
- 3.2.9.RELEASE
- 3.2.10.RELEASE
- 3.2.11.RELEASE
- 3.2.12.RELEASE
- 3.2.13.RELEASE
- 3.2.14.RELEASE
- 3.2.15.RELEASE
- 3.2.16.RELEASE
- 3.2.17.RELEASE
- 3.2.18.RELEASE
- 4.0.0.RELEASE
- 4.0.1.RELEASE
- 4.0.2.RELEASE
- 4.0.3.RELEASE
- 4.0.4.RELEASE
- 4.0.5.RELEASE
- 4.0.6.RELEASE
- 4.0.7.RELEASE
- 4.0.8.RELEASE
- 4.0.9.RELEASE
- 4.1.0.RELEASE
- 4.1.1.RELEASE
- 4.1.2.RELEASE
- 4.1.3.RELEASE
- 4.1.4.RELEASE
- 4.1.5.RELEASE
- 4.1.6.RELEASE
- 4.1.7.RELEASE
- 4.1.8.RELEASE
- 4.1.9.RELEASE
- 4.2.0.RELEASE
- 4.2.1.RELEASE
- 4.2.2.RELEASE
- 4.2.3.RELEASE
- 4.2.4.RELEASE
- 4.2.5.RELEASE
- 4.2.6.RELEASE
- 4.2.7.RELEASE
- 4.2.8.RELEASE
- 4.2.9.RELEASE
- 4.3.0.RELEASE
- 4.3.1.RELEASE
- 4.3.2.RELEASE
- 4.3.3.RELEASE
- 4.3.4.RELEASE
- 4.3.5.RELEASE
- 4.3.6.RELEASE
- 4.3.7.RELEASE
- 4.3.8.RELEASE
- 4.3.9.RELEASE
- 4.3.10.RELEASE
- 4.3.11.RELEASE
- 4.3.12.RELEASE
- 4.3.13.RELEASE
- 4.3.14.RELEASE
- 4.3.15.RELEASE
- 4.3.16.RELEASE
- 4.3.17.RELEASE
- 4.3.18.RELEASE
- 4.3.19.RELEASE
- 4.3.20.RELEASE
- 4.3.21.RELEASE
- 4.3.22.RELEASE
- 4.3.23.RELEASE
- 4.3.24.RELEASE
- 4.3.25.RELEASE
- 4.3.26.RELEASE
- 4.3.27.RELEASE
- 4.3.28.RELEASE
- 4.3.29.RELEASE
- 4.3.30.RELEASE
Versions in green have been tested.
- log4j 1.2.17 (Apache Log4j 1.2)
- JDK 1.8
- Maven 3.3.9
|