Wednesday, September 30, 2009

Setup java.util.logging in WAS client (unit test)

If you run WebSphere client it is very useful to have client logging set up. Recently I had the same need when I run some JUnit integration test. In that test I instantiated SCA POJO component that had a lot of log statements logged in. Naturally I wanted to have those log message printed out on console when test is run. After short googling I found out that there are two (easiest) options available:
  1. Using java.util.logging.LogManager built in functionality reading initial configuration from logging.properties file
  2. Enabling trace on client using IBM trace support using trace settings file
I created junit run configuration using both options. There is one issue that I experienced though, I was unable to use eclipse variables like "workspace_loc". I believe that this is because this variable has windows file syntax that is not best understood when reading property file. So when defining run configuration you should use bin (or other output directory) as working directory under Arguments tab. Property file is placed in java source folder.

When using logging.properties I had
-Djava.util.logging.config.file=logging.properties
under VM arguments. My logging.properties has content:
# Add handlers to the root logger.
# These are inherited by all other loggers.
handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler

# Set the logging level of the root logger.
# Levels from lowest to highest are
# FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE.
# The default level for all loggers and handlers is INFO.
.level=INFO

# Specify logging levels for specific namespaces.
org.example.level=FINEST

# Configure the ConsoleHandler.
# ConsoleHandler uses java.util.logging.SimpleFormatter by default. 
# Even though the root logger has the same level as this,
# the next line is still needed because we're configuring a handler,
# not a logger, and handlers don't inherit properties from the root logger.
java.util.logging.ConsoleHandler.level=FINEST

# Configure the FileHandler.
# FileHandler uses java.util.logging.XMLFormatter by default. 
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=FINEST

# The following special tokens can be used in the pattern property
# which specifies the location and name of the log file.
#   / - standard path separator
#   %t - system temporary directory
#   %h - value of the user.home system property
#   %g - generation number for rotating logs
#   %u - unique number to avoid conflicts
# FileHandler writes to %h/demo0.log by default.
java.util.logging.FileHandler.pattern=E:/temp/org_example_test.log

For other run configuration, using IBM trace settings file under VM settings I have
-DtraceSettingsFile=TraceSettings.properties -Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager  -Djava.util.logging.configureByServer=true
  Where TraceSetting.properties has content:
# 5724-I63, 5724-H88 (C) COPYRIGHT International Business Machines Corp. 2000, 2004
# All Rights Reserved * Licensed Materials - Property of IBM


########################################################################
# A property to specify where the output messages and trace (if enabled)
# should be routed. Valid values include stdout or a fully qualified
# file name. If a file is specified but cannot be opened, stdout is used.
# 
# Sample invocations
# To specify stdout - traceFileName=stdout
# To specify a file on NT - traceFileName=c:\\MyTraceFile.log or
#                           traceFileName=c:/MyTraceFile.log
# To specify a file on Unix - traceFileName=/usr/MyTraceFile.log
########################################################################
#traceFileName=e:\\test\\MyTraceFile.log
traceFileName=stdout

########################################################################
# Specify trace strings here. Trace strings take the form of:
# logger={level}={type} where:
#     level = entryexit || debug || event || all
#     type =  enabled || disabled
# examples:
# com.ibm.ejs.ras.SharedLogBase=all=enabled enables all tracing for the
#         single logger created in class com.ibm.ejs.ras.SharedLogBase.
# com.ibm.ejs.*=debug=enabled enables debug tracing for all loggers with
#         names starting with com.ibm.ejs.
#
# Multiple trace strings can be specified, one per line.
########################################################################
org.example.*=all=enabled