6.10. Logging in Java clients

6.10. Logging in Java clients

The Java client software supports logging using the Simple Logging Facade for Java (SLF4J), a facade that supports popular logging systems like log4j version 1.2.x, JDK 1.4 logging, Simple logging, and NOP.

This section discusses logging using log4j. Make sure your CLASSPATH contains log4j.1.2.x.jar,e.g. by installing it with yum or by setting the variable by hand..

Log4j requires an initialization file, whose location should be specified as a URL using the log4j.configuration system property, e.g:

-Dlog4j.configuration=file://home/fidget/java/log4j.xml

Let's look at log4j.xml, a log4j configuration file shipped in the java examples directory. This property filedefines two output destinations, which log4j calls “appenders”. One of these writes to a file called “qpid_messaging.log”, the other writes to standard output.

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="FileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="qpid_messaging.log"/>
        <param name="Append" value="false"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%t %-5p %c{2} - %m%
n"/>
        </layout>
    </appender>

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%
F:%L) - %m%n"/>
        </layout>
    </appender>
    <!-- continued below .... -->

This file also uses a separate logger for warning messages in the package org.apache, printing messages at level warn and above. (log4j uses the following hierarchical set of logging levels: trace, debug, info, warn, error and fatal.)

    <logger name="org.apache">
        <!-- Print only messages of level warn or above in the package
org.apache -->
        <level value="warn"/>
    </logger>
    <!-- continued below .... -->

In log4j, each enabled 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. The root appender is at the root of the hierarchy, so the following declaration ensures that all logging requests are printed to stdout:

    <root>
        <priority value="info"/>
        <appender-ref ref="STDOUT"/>
        <!-- <appender-ref ref="FileAppender"/> -->
    </root>
</log4j:configuration>

To ensure that all logging requests are also sent to the FileAppender, simply uncomment that element.

More log4j documentation can be found at http://logging.apache.org/log4j/1.2/manual.html