Logging quickstart: Log4j and Jakarta Commons logging

1

Jakarta Commons logging is a wrapper around Log4j (Log4jLogger), the Java native logging system(Jdk14Logger) and the built-in fall-back logger SimpleLog. Depending on which one is on the classpath, that logging system is addressed transparently via the easy-to-learn commons logging API.

Recipe:

  1. Create a log4j.properties such as
    log4j.rootLogger=DEBUG, stdout
    log4j.logger.my.package.name=DEBUG
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{dd-MM HH:mm:ss,SSS} %-5p (%F:%M:%L)  -%m%n
    

    Fill in my.package.name; if you have a packages x.y.z1 and x.y.z2, and want to turn loggin on for both, x.y suffices. You may want to adjust the logging level as well.

  2. When developing web apps, make sure this file is put in WEB-INF/classes. When developing in Eclipse, you may put it with your sources, it will be put in this directory automatically and hence deployed correctly.
  3. Add the commons-logging.jar and the log4j-1.2.8.jar in your WEB-INF/lib directory
  4. Add the following member variable to your class:
    private static Log log = LogFactory.getLog(MyCurrentClass.class);
    

    filling in MyCurrentClass with the class in which the logging is to be used.

  5. Add the log.debug(), log.trace() etc. messages as needed.

Warning: not placing the right commons-logging.jar in the WEB-INF/lib directory can lead to mysterious behaviour ;-)

See also: Jakarta Logging – commons.

Share.

About Author

1 Comment

  1. Pingback: Surfboard Superstore