Apache log4j – log4j.properties Configuration

Apache log4j is commonly used in Java for logging purpose. This post guides you how to make use of it in your Java program.

In a Maven project, just add the following dependency in the pom.xml.

...
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.16</version>
  <exclusions>
    <exclusion>
      <groupId>com.sun.jdmk</groupId>
      <artifactId>jmxtools</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.jmx</groupId>
      <artifactId>jmxri</artifactId>
    </exclusion>
  </exclusions>
</dependency>
...

 

Next, add the following code inside the Java files where you want to have logs.

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleApp {
  private static final Log logger = LogFactory.getLog(SimpleApp.class);
	
  public static void main(String[] args) {
    logger.debug("SimpleApp DEBUG");
    logger.info("SimpleApp INFO");
    logger.warn("SimpleApp WARN");
    logger.error("SimpleApp ERROR");
    logger.fatal("SimpleApp FATAL");
  }
}

 

As you can see, there are five level of logs which are DEBUG, INFO, WARN, ERROR and FATAL. The level of log is determined inside the log4j.properties. If the level is DEBUG, all log messages will be printed. If the level is INFO, then no DEBUG log will be printed and so on.

So now, create the log4j.properties under <maven project>/src/main/resources as follow

log4j.rootLogger=DEBUG, CONSOLE
#log4j.rootLogger=INFO, FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=log4j.log
log4j.appender.FILE.MaxFileSize=512KB
log4j.appender.FILE.MaxBackupIndex=3
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

 

In the above configuration, you can either print the log to stderr by log4j.rootLogger=DEBUG, CONSOLE or create a log4j.log file in your Maven project folder by log4j.rootLogger=INFO, FILE.

The conversionPattern determines the log message structure. The above configuration will print a log like this

2010-03-26 23:48:03,234 DEBUG - SimpleApp DEBUG
2010-03-26 23:48:03,234 INFO  - SimpleApp INFO
2010-03-26 23:48:03,234 WARN  - SimpleApp WARN
2010-03-26 23:48:03,234 ERROR - SimpleApp ERROR
2010-03-26 23:48:03,234 FATAL - SimpleApp FATAL

For more information about the conversionPattern. Please refer to log4j apidocs

Done =)

14 thoughts on “Apache log4j – log4j.properties Configuration”

  1. I have a problem setting up a log4j.xml config file (for commons logging) in my maven project.

    This is what I did.
    1) Added a commons-logging dependency into pom.xml
    2) Added a resource log4j.xml in pom.xml
    3) Added log statements in the class

    But I saw that the pattern of log messages in not what I set in my log4j.xml. It seemed it picked some other config file.

    To overcome that,
    1) I added commons-logging.properties in the classpath, which set log4j.configuration=wcp-log4j.xml (I renamed the log4j.xml file to my custom one).

    Still no luck.
    So I searched for log4j jars in my local maven repository and found that there were some four log4j jars. So in my pom.xml I excluded all these jars against my dependency of commons-logging.

    Still no luck. It still prints the messages with the default pattern. So could you please guide me how to resolve this?

    Like

  2. 4.0.0

    base
    com.cgi.wealth
    1.0-SNAPSHOT

    com.cgi.wealth
    base-common
    1.0-SNAPSHOT

    aspectj
    aspectjweaver
    1.5.4
    compile

    commons-logging
    commons-logging
    1.1.1

    slf4j-log4j12
    org.slf4j

    log4j
    log4j

    maven-plugin-log4j
    com.pyx4j

    jar
    compile

    src/main/resources

    wcp-log4j.xml
    commons-logging.properties

    Like

    1. How did u run the webapp? u deploy the built .war to a web container or u just start it using the maven-jetty-plugin?

      Like

  3. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd>
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>base</artifactId>
    <groupId>com.cgi.wealth</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.cgi.wealth</groupId>
    <artifactId>base-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
    <dependency>
    <groupId>aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.5.4</version>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
    <exclusions>
    <exclusion>
    <artifactId>slf4j-log4j12</artifactId>
    <groupId>org.slf4j</groupId>
    </exclusion>
    <exclusion>
    <artifactId>log4j</artifactId>
    <groupId>log4j</groupId>
    </exclusion>
    <exclusion>
    <artifactId>maven-plugin-log4j</artifactId>
    <groupId>com.pyx4j</groupId>
    </exclusion>
    </exclusions>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    </dependencies>
    <build>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <includes>
    <include>wcp-log4j.xml</include>
    <include>commons-logging.properties</include>
    </includes>
    </resource>
    </resources>
    </build>

    </project>

    Like

    1. by the way, u dun need to exclude the log4j dependency in the pom.xml. it is normal the your local maven repository having more than one dependency jar files with different versions.

      Like

  4. I notice that in your log4j.properties, you have the rootLogger as DEBUG, but in your example output, the DEBUG message isn’t printed.

    I found your page because I was looking for a solution to this very problem. Despite having DEBUG set in my log4j.properties, I can’t get the .debug messages to print

    Any ideas? My setup is very similar to yours. I’m using tomcat:run to run the web application. It’s a Struts2 application.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.