Spring - Logging in Log4j

[Last Updated: Sep 10, 2017]

Log4j dependency

To use Log4j with the JCL API (the default spring logging dependency), we need to add Log4j dependency:


We also have to provide in classpath.


log4j.rootCategory=INFO, stdout
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() {"doing something");

Main class

public class ExampleMain {

  public MyBean myBean() {
      return new MyBean();

  public static void main(String[] args) {
      ConfigurableApplicationContext context =
              new AnnotationConfigApplicationContext(ExampleMain.class);

      MyBean bean = context.getBean(MyBean.class);


17-05-24 21:12:26:826  INFO main annotation.AnnotationConfigApplicationContext:582 - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1376c05c: startup date [Wed May 24 21:12:26 CDT 2017]; root of context hierarchy
17-05-24 21:12:27:014 INFO main example.MyBean:10 - In MyBean#doSomething() method

Directly Using Log4j API

Instead of using JCL API, we can use Log4j API directly:

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() {"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.

Example Project

Dependencies and Technologies Used:

  • spring-context 4.3.8.RELEASE: Spring Context.
  • log4j 1.2.17: Apache Log4j 1.2.
  • JDK 1.8
  • Maven 3.3.9

Spring Log4j Loggin Select All Download
  • spring-log4j
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • resources

    See Also