Use a logger

There are various ways in which to log messages or trace using Java logging. The following guidelines will help you to use Java logging to log messages and add tracing:

  1. Use level WsLevel and above for messages, and lower levels for Trace. The WAS Extension API (the com.ibm.websphere.logging package) contains the WsLevel class.

    • For messages use

      WsLevel.FATAL 
      Level.SEVERE 
      Level.WARNING 
      WsLevel.AUDIT 
      Level.INFO 
      Level.CONFIG 
      WsLevel.DETAIL
      
      

    • For trace use

      Level.FINE 
      Level.FINER 
      Level.FINEST
      

  2. Use the logp method instead of log or logrb since logp accepts parameters for class name and method name. The log and logrb methods will generally try to infer this information, but the performance penalty is prohibitive.

  3. Avoid using the logrb method since this leads to inefficient caching of resource bundles which results in poor performance.

  4. Use the isLoggable method to avoid creating data for a logging call that will not get logged. For example

    if (logger.isLoggable(Level.FINEST)) {
       String s = dumpComponentState();  // some expensive to compute method
       logger.logp(Level.FINEST, className, methodName, "componentX state 
    dump:\n{0}", s);
     }
    

The following sample applies to localized messages

// note - generally avoid use of FINE, FINER, FINEST levels for messages to be consistent with WebSphere Application Server

String componentName = "com.ibm.websphere.componentX";
String resourceBundleName = "com.ibm.websphere.componentX.Messages";
Logger logger = Logger.getLogger(componentName, resourceBundleName);

// "Convenience" methods - not generally recommended due to lack of class 
/ method names
//   - can't specify message substitution parameters
//   - can't specify class/method names
if (logger.isLoggable(Level.SEVERE))
  logger.severe("MSG_KEY_01");

if (logger.isLoggable(Level.WARNING))
  logger.warning("MSG_KEY_01");

if (logger.isLoggable(Level.INFO))
  logger.info("MSG_KEY_01");

if (logger.isLoggable(Level.CONFIG))
  logger.config("MSG_KEY_01");


// "log" methods - not generally recommended due to lack of class / method 
names
//   - enable use of WAS specific levels
//   - enable use of message substitution parameters
//   - can't specify class/method names
if (logger.isLoggable(WsLevel.FATAL))
  logger.log(WsLevel.FATAL, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.SEVERE))
  logger.log(Level.SEVERE, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.WARNING))
  logger.log(Level.WARNING, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(WsLevel.AUDIT))
  logger.log(WsLevel.AUDIT, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.INFO))
  logger.log(Level.INFO, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.CONFIG))
  logger.log(Level.CONFIG, "MSG_KEY_01", "parameter 1");

if (logger.isLoggable(WsLevel.DETAIL))
  logger.log(WsLevel.DETAIL, "MSG_KEY_01", "parameter 1");


// "logp" methods - the recommended way to log
//   - enable use of WAS specific levels
//   - enable use of message substitution parameters
//   - enable use of class/method names
if (logger.isLoggable(WsLevel.FATAL))
  logger.logp(WsLevel.FATAL, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(Level.SEVERE))
  logger.logp(Level.SEVERE, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(Level.WARNING))
  logger.logp(Level.WARNING, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(WsLevel.AUDIT))
  logger.logp(WsLevel.AUDIT, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(Level.INFO))
  logger.logp(Level.INFO, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(Level.CONFIG))
  logger.logp(Level.CONFIG, className, methodName, "MSG_KEY_01", 
"parameter 1");

if (logger.isLoggable(WsLevel.DETAIL))
  logger.logp(WsLevel.DETAIL, className, methodName, "MSG_KEY_01", 
"parameter 1");


// "logrb" methods - not generally recommended due to diminished performance 
of switching resource bundles on the fly
//   - enable use of WAS specific levels
//   - enable use of message substitution parameters
//   - enable use of class/method names
String resourceBundleNameSpecial = 
"com.ibm.websphere.componentX.MessagesSpecial";

if (logger.isLoggable(WsLevel.FATAL))
  logger.logrb(WsLevel.FATAL, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.SEVERE))
  logger.logrb(Level.SEVERE, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.WARNING))
  logger.logrb(Level.WARNING, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(WsLevel.AUDIT))
  logger.logrb(WsLevel.AUDIT, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.INFO))
  logger.logrb(Level.INFO, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(Level.CONFIG))
  logger.logrb(Level.CONFIG, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");

if (logger.isLoggable(WsLevel.DETAIL))
  logger.logrb(WsLevel.DETAIL, className, methodName, resourceBundleNameSpecial, 
"MSG_KEY_01", "parameter 1");


For trace, (or content that is not localized), the following sample applies

// note - generally avoid use of FATAL, SEVERE, WARNING, AUDIT, INFO, CONFIG, 
DETAIL levels for trace 
// to be consistent with WebSphere Application Server

String componentName = "com.ibm.websphere.componentX";
Logger logger = Logger.getLogger(componentName);

// Entering / Exiting methods - recommended for non trivial methods
if (logger.isLoggable(Level.FINER))
  logger.entering(className, methodName);
  
if (logger.isLoggable(Level.FINER))
  logger.entering(className, methodName, "method param1");

if (logger.isLoggable(Level.FINER))
  logger.exiting(className, methodName);

if (logger.isLoggable(Level.FINER))
  logger.exiting(className, methodName, "method result");


// Throwing method - not generally recommended due to lack of message - use 
logp with a throwable parameter instead
if (logger.isLoggable(Level.FINER))
  logger.throwing(className, methodName, throwable);


// "Convenience" methods - not generally recommended due to lack of class 
/ method names
//   - can't specify message substitution parameters
//   - can't specify class/method names
if (logger.isLoggable(Level.FINE))
  logger.fine("This is my trace");

if (logger.isLoggable(Level.FINER))
  logger.finer("This is my trace");

if (logger.isLoggable(Level.FINEST))
  logger.finest("This is my trace");


// "log" methods - not generally recommended due to lack of class / 
method names
//   - enable use of WAS specific levels
//   - enable use of message substitution parameters
//   - can't specify class/method names
if (logger.isLoggable(Level.FINE))
  logger.log(Level.FINE, "This is my trace", "parameter 1");

if (logger.isLoggable(Level.FINER))
  logger.log(Level.FINER, "This is my trace", "parameter 1");

if (logger.isLoggable(Level.FINEST))
  logger.log(Level.FINEST, "This is my trace", "parameter 1");


// "logp" methods - the recommended way to log
//   - enable use of WAS specific levels
//   - enable use of message substitution parameters
//   - enable use of class/method names
if (logger.isLoggable(Level.FINE))
  logger.logp(Level.FINE, className, methodName, "This is my trace", 
"parameter 1");

if (logger.isLoggable(Level.FINER))
  logger.logp(Level.FINER, className, methodName, "This is my trace", 
"parameter 1");

if (logger.isLoggable(Level.FINEST))
  logger.logp(Level.FINEST, className, methodName, "This is my trace", 
"parameter 1");


// "logrb" methods - not applicable for trace logging since no localization 
is involved