Use a logger
You can use Java logging to log messages and add tracing.
Overview
Use WsLevel.DETAIL level 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.DETAILFor trace use:Level.FINE Level.FINER Level.FINEST
Procedure
- Use the logp method instead of the log or the logrb method. The logp method 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. In general, the logp method has less performance impact than the log or the logrb method.
- Avoid using the logrb method. This method leads to inefficient caching of resource bundles and poor performance.
- Use the isLoggable method to avoid creating data for a logging call that does 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); }
Example
The following sample applies to localized messages:// note - generally avoid use of FINE, FINER, FINEST levels for messages to be consistent with // WAS 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 // - cannot specify message substitution parameters // - cannot specify class and 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 are not generally used due to lack of class and method names // - enable use of WAS-specific levels // - enable use of message substitution parameters // - cannot specify class and 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 are the way to log // - enable use of WAS-specific levels // - enable use of message substitution parameters // - enable use of class and 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 are not generally used due to diminished performance of switching resource bundles dynamically // - enable use of WAS-specific levels // - enable use of message substitution parameters // - enable use of class and 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 WAS String componentName = "com.ibm.websphere.componentX"; Logger logger = Logger.getLogger(componentName); // Entering / Exiting methods are used 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 is not generally used due to lack of message - use logp with a throwable parameter instead if (logger.isLoggable(Level.FINER)) logger.throwing(className, methodName, throwable); // Convenience methods are not generally used due to lack of class / method names // - cannot specify message substitution parameters // - cannot specify class and 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 are not generally used due to lack of class and method names // - enable use of WAS-specific levels // - enable use of message substitution parameters // - cannot specify class and 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 are the recommended way to log // - enable use of WAS-specific levels // - enable use of message substitution parameters // - enable use of class and 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 are not applicable for trace logging because no localization is involved
Configure the logger hierarchy
Creating log resource bundles and message files
Related Reference
Example: Creating custom log handlers with java.util.logging
Example: Creating custom filters with java.util.logging
Example: Creating custom formatters with java.util.logging
Example: Adding custom handlers, filters, and formatters