My hacking journal

Java logging with log4j

· by admin · Read in about 3 min · (492 Words)
java log4j logging

What is a Logger? Basically it’s an object which lets you write code like

logger.debug("Just entered main");
logger.error(new FileNotFoundException("/home/foo/bar.txt"));
logger.warn(new Integer(12921));

instead of all those System.out.println to log what your program does. With log4j one can conveniently log to stdout, a rolling file, the syslog daemon, a message queue and so on.

How to get a Logger object? Call the static method Logger.getLogger(String name). As you can see, when you get a Logger you explicitely supply an identifier. This name is used to provide context to logging calls, so they can be effectively filtered depending on applied settings. Logger names are arranged in a hierarchical tree based on their dot separated names, exactly like DNS works: is the parent of, which is a child and a descendant of both com and Instead «com» alone is said to be an ancestor of The hierarchy matters because child loggers inherits some properties from their ancestors, like level and default appenders.

Logging is usually configured in a property files, sometimes to give users a chance for customization, but more often just to tell declarative code apart. So we put this somewhere in the CLASSPATH (sometimes it can be useful to start the VM with the system property -Dlog4j.debug to see what configuration file log4j is picking to configure itself):

# Declare a logger named "com.zybnet", and add
# two named appenders: "goku" and "vegeta".
# The first argument, "debug", is optional and specifies
# the minimum level for the message to be logged, goku, vegeta

# Declare the "goku" appender and define it
log4j.appender.goku.layout.ConversionPattern=[%-5p] %m%n

# Declare the "vegeta" appender
log4j.appender.vegeta.layout.Title=My fantastic app log

The code is quite well commented, you should intuitively understand what an «appender» is, what a «layout» is and how a named logger is created and configured. The only things that should be noted is that this file is interpreted by the PropertyConfigurator after the JavaBean standard: when you see setSomething() and getSomething() in the Javadoc, there is a property named Something that can be configured (examples are vegeta.File and layout.ConversionPattern). Remeber to traverse the Java class hierarchy to discover properties defined by superclasses.

Here is a sample Java demo which exercise the log4j API and also illustrates how to get a logger via a class name

package com.zybnet;


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Main {

    static Logger logger;

    static {
        logger = Logger.getLogger(Main.class);

    public static void main(String[] args) {
        logger.debug("Just entered main");
        logger.error(new FileNotFoundException("/home/foo/bar.txt"));
        logger.warn(new Integer(12921));


Here are some useful links:

* [Manual](

* [PropertyConfigurator Javadoc](

* [PatternLayout](

This is a quick reference to understand basic log4j usage. If one needs more specialized behavior, it can certainly understand the official doc itself ;)

To disable a logger you use OFF (it’s kind of an undocumented log level)

Todo: Often log4j is paired with slf4j and commons-logging. Explore these things.