Log4j

 
 
Log4j is a package to output log statements to a variety of output targets.

The log statements can remain in your shipped code without incurring a heavy performance cost. The logging behavior can be controlled by editing a configuration file (log4j.properties), without modifying the application.

One of the distinctive features of log4j is the notion of inheritance in loggers. By using a logger hierarchy it is possible to control which log statements are output at arbitrarily fine granularity. This helps reduce the volume of logged output and minimize the cost of logging.

The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or even a NT Event logger among many other output targets.

Log4j has three main components (loggers, appenders and layouts) which works together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.

Log4j is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

More information about log4j can be found at http://logging.apache.org/log4j/docs/index.html

The latest Log4j version can be downloaded from http://logging.apache.org/log4j/docs/download.html







Using layouts.



Information
To customize the output format of the print messages a layout must be associated to an appender. There are serveral layouts you can choose:
  • ObjectRenderer.
    The ObjectRenderer render objects as strings.

  • PatternLayout.
    The PatternLayout lets you specify the output format according to conversion patterns similar to the C language printf function. A conversion pattern is composed of literal text and format control expressions called conversion specifiers. Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. Here below serveral examples:

    log4j.appender.stdout.layout.ConversionPattern=%-5p [%t]: %m%n
    ERROR [http-8080-Processor5]: This is my error message.

    log4j.appender.stdout.layout.ConversionPattern=%d [%c{1}] %p - %m%n
    2005-03-26 13:00:54,654 [DemoAction] ERROR - This is my error message.

    log4j.appender.stdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} %-5p [%c] - %m%n
    26 mrt 2005 13:06:36 ERROR [com.mobilefish.DemoAction] - This is my error message.

    log4j.appender.stdout.layout.ConversionPattern=%5p [%C:%M] (%F:%L) - %m%n
    ERROR [com.mobilefish.DemoAction:execute] (DemoAction.java:68) - This is my error message.

    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %r %5p (%l) - %m%n
    13:38:42,976 10 ERROR (com.mobilefish.DemoAction.execute(DemoAction.java:69)) - This is my error message.


    An overview of conversion characters:

    Conversion Character Effect
    c Used to output the category of the logging event.
    The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

    If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed.
    By default the category name is printed in full.

    For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
    C Used to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.

    If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed.
    By default the class name is output in fully qualified form.

    For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".

    WARNING. Generating the caller class information is slow. Thus, it's use should be avoided unless execution speed is not an issue.
    d Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed.

    The date format specifier admits the same syntax as the time pattern string of the java.text.SimpleDateFormat. Although part of the standard JDK, the performance of SimpleDateFormat is quite poor.

    For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectively ISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE}.

    These dedicated date formatters perform significantly better than SimpleDateFormat.
    F Used to output the file name where the logging request was issued.

    WARNING. Generating caller location information is extremely slow.
    It's use should be avoided unless execution speed is not an issue.
    l Used to output location information of the caller which generated the logging event.

    The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses.

    The location information can be very useful. However, it's generation is extremely slow. It's use should be avoided unless execution speed is not an issue.
    L Used to output the line number from where the logging request was issued.

    WARNING. Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.
    m Used to output the application supplied message associated with the logging event.
    M Used to output the method name where the logging request was issued.

    WARNING. Generating caller location information is extremely slow. It's use should be avoided unless execution speed is not an issue.
    n Outputs the platform dependent line separator character or characters.

    This conversion character offers practically the same performance as using non-portable line separator strings such as "\n", or "\r\n".
    Thus, it is the preferred way of specifying a line separator.
    p Used to output the priority of the logging event.
    r Used to output the number of milliseconds elapsed since the start of the application until the creation of the logging event.
    t Used to output the name of the thread that generated the logging event.
    x Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
    X Used to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output.
    % The sequence %% outputs a single percent sign.


    An overview of format modifiers:

    Format modifier left justify minimum width maximum width comment
    %20c false 20 none Left pad with spaces if the category name is less than 20 characters long.
    %-20c true 20 none Right pad with spaces if the category name is less than 20 characters long.
    %.30c NA none 30 Truncate from the beginning if the category name is longer than 30 characters.
    %20.30c false 20 30 Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning.
    %-20.30c true 20 30 Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning.
Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Log4j 1.2.9

Procedure
  1. An example to assign a layout to an appender in the log4j.properties configuration file:

    log4j.rootLogger=ERROR, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%r [%t] %-5p %c - %m%n


    Output: 176 [main] INFO org.foo.Bar - Located nearest gas station.

    The first field is the number of milliseconds elapsed since the start of the program.
    The second field is the thread making the log request.
    The third field is the level of the log statement.
    The fourth field is the name of the logger associated with the log request.
    The text after the '-' is the message of the statement.