Appendix B. Logging and JBoss

Most J2EE applications use some form of logging or tracing API that stores messages to a persistent medium (such as DBMS or file). Logging provides the following benefits:

Debugging

Developers generate log messages to debug their programs.

Reviewing

System personnel examine log messages to check for problems.

Auditing

Security personnel can review log messages to see what actions a user performed in the system.

This chapter covers two of the most common logging APIs and how to use them with JBoss:

  • Jakarta Commons Logging (JCL) API

  • Apache Log4J

Jakarta Commons Logging (JCL) API

The Jakarta Commons Logging (JCL) package from the Apache Jakarta project provides a standard interface to the various logging libraries, hiding the details of the logging implementation from an application. Many logging APIs are available, and at some point an application may need to change to another logging technology. The main benefit of the JCL is that it enables developers to switch between logging implementations without impacting their code.

Using the Apache JCL

Example B-1 is an example from the JAW Motors InitServlet that uses the JCL to print out a log message.

Example B-1. InitServlet.java
...

import org.apache.commons.logging.*;

...

public class InitServlet extends GenericServlet {
    private Log log = LogFactory.getLog(InitServlet.class);

    private ServletContext servletContext;

    public void init() {
        ...
        log.info("Testing Logging Setup ...");
    }
}

The call to LogFactory.getLog() creates ...

Get JBoss at Work: A Practical Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.