Log4j features and a quick start example

[Updated: Jun 8, 2017, Created: May 28, 2017]

Followings are the main components of the Log4j framework.

Logger: Developers use this class to print log messages. These methods are used: debug, info, warn, fatal and log. Each of these methods are associated with a Level.
Appender: This specifies the strategies for outputting logs to a destination. For example, ConsoleAppender sends log events to System.out or System.err. More than one appender can be attached to a logger.
Layout: An implementation of this (e.g. PatternLayout) specifies a format of the logs.

How things work?

  • Logger hierarchy:

    Loggers are named entities. They are organised in hierarchy form. A parent Logger can be based on a package name (say com.logicbig); that means all classes in that package will be the child loggers, also all nested packages (say com.logicbig.example) will also be it's children.

  • Root logger:

    This logger resides at the top of the logger hierarchy. This logger always exists and cannot be retrieved by name. It can be retrieved by static method: Logger#getRootLogger().

  • Hierarchy and appenders:

    Appenders are inherited. That means each logging request for a given logger, will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy.

  • Level:

    A level is assigned to the logger instance or it can be specified when calling one of the logging methods of Logger class.

  • Hierarchy and levels:

    A level assigned to a parent logger will be inherited by its children. A child can override that by assigning its own level.

  • Disabling certain logs:

    Levels are ordered. The standard levels are, in lower to higher order: DEBUG, INFO, WARN, ERROR and FATAL. A logging request (calling logging methods of Logger) is said to be enabled, if the specified Level of the request, is higher than or equal to the assigned Level of its logger. Otherwise, the request is said to be disabled.

  • Calling Logger.getLogger(Example.class):

    This static method (and other similar overloaded methods) will return the existing logger instance. If it's the first time call, then new instance is created and cached (lazy loading). The new logger will take the name of the specified class. Child loggers are only linked to their existing parents. For example, the logger named com.logicbig.example.Example is directly linked to the 'root logger', hence skipping the creation of unnecessary com or com.logicbig or com.logicbig.example loggers. If anyone of intermediate parents has already been created by some other class then it will be included in the linkage.

  • Configuration:

    The log4j environment can fully be configured programmatically or by configuration files (log4j.xml or log4j.properties).

  • Disabling logs for some packages/classes:

    
    log4j.logger.<some package/class> = DEBUG|INFO|OFF|WARN...


A quick example

pom.xml

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.17</version>
</dependency>

src/main/resources/log4j.properties

log4j.rootLogger=INFO, xyz
log4j.appender.xyz=org.apache.log4j.ConsoleAppender
log4j.appender.xyz.layout=org.apache.log4j.PatternLayout
log4j.appender.xyz.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n
package com.logicbig.example;

import org.apache.log4j.Logger;

public class Example {
  final static Logger LOGGER = Logger.getLogger(Example.class);

  public static void main(String[] args) {
      LOGGER.info("a test message");
  }
}

Output

17-05-28 12:54:18:488  INFO main example.Example:9 - a test message

Example Project

Dependencies and Technologies Used :

  • log4j 1.2.17: Apache Log4j 1.2.
  • JDK 1.8
  • Maven 3.3.9

Log4j Getting Started Example Select All Download
  • log4j-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also