Java Util Logging Behaviour in WebSphere

Logging is the interface between developers and system admin. Developers need to know how their logging code behave in a server envrionment. Admins need to know how to configure and turn on/off logging. This series inspects how two most common logging methods, Java Util Logging and Log4j, behave in Tomcat and WebSphere.

Java Util Logging is part of the JDK starting with version 6, which means that a web application can use it without including any additional jars in WEB-INF/lib. However, also because it is part of the JDK, WebSphere itself uses the library for internal logging. How to protect WebSphere internal logging from misconfiguration? WebSphere has implemented extra control over the Java Util Logging framework. This article inspects how Java Util Logging behaves in WebSphere.

1. Default Behaviour with Basic Logging Code

  • Basic Java Util Logging Code

In the following java util logging sample code, two loggers are created: the mainLogger com.appinf and a perfSubLogger com.appinf.perf.  This is to help us understand how java util logging organizes loggers in a tree structure. No additional calls are made to customize these loggers
public class JavaUtilLoggingTestServlet extends HttpServlet {
    static Logger mainLogger = null;
    static Logger perfSubLogger = null;
    static {
        mainLogger = Logger.getLogger("com.appinf");
        perfSubLogger = Logger.getLogger("com.appinf.perf");
    protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final long startTime = System.currentTimeMillis();
        perfSubLogger.logp(Level.INFO, "JavaUtilLoggingSETest", "main", "Enter");
        mainLogger.logp(Level.INFO, "JavaUtilLoggingSETest", "main", "Processing ... ");

        final long endTime = System.currentTimeMillis();
        perfSubLogger.logp(Level.INFO, "JavaUtilLoggingSETest", "main", "Exit Total time : " + (endTime - startTime) + " ms.");

        String destination = "/test.jsp";
        getServletContext().getRequestDispatcher(destination).forward(request, response);
  • How does Basic Logging Code Behave in WebSphere

1. WebSphere Log Manager :

In Java SE, Logger.getLogger invokes java.util.logging.LogManager, which creates all loggers within the JVM based on a .properties file. WebSphere, however, uses java util logging for its own internal components, as complex and crucial as transactions, messaging etc., so it doesn't want users to have the chance to misconfigure the .properties file. Instead, it manages loggers using its own log manager by adding the following system property to all WebSphere jvms.

2. Logging configuration is managed internally. I don't know of a .config file similiar to the one in Java SE or Tomcat.

3. By default, all loggers will log to System.out.

4. WebSphere does provide a nice interface to change the logging level through admin console.

2. Customize : Log to application specific log files

If the default behaviour is not what you want, and you want applications to write to their own logs, you can only do it in Java code. This is documented in WAS Info Center.

Add a FileHandler in Java Code.   

if ( mainLogger.getHandlers().length == 0){
    FileHandler fh = new FileHandler("logs" 
        + File.separator + "com.appinf.log", 1024 * 5000, 10, true);
    fh.setFormatter(new SimpleFormatter());        
There are several things to note here.
  • If you are not using absolute path, it will use the profile home directory profiles\was70profile1. So the above code will log to profileHome\logs\com.appinf.log, ususally the right place to be. 
  • Even though you specified a file handler, logs are still written to the parent logger, which is System.out. To disable it, you have to setUseParentHandlers to false.


No comments:

Post a Comment