Logging quickstart: Log4j and Jakarta Commons logging

admin 1
0 0
Read Time:1 Minute, 15 Second

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.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “Logging quickstart: Log4j and Jakarta Commons logging

Comments are closed.

Next Post

Enhancements for JHeadstart Generator/Consolidation of best practices

This post describes a list of JHeadstart Application Generator enhancements and also provides a progress report on the implementation of the features mentioned
%d bloggers like this: