Example: Adding custom handlers, filters, and formatters

In some cases you might want to have your own custom log files. Adding custom handlers, filters, and formatters enables you to customize your logging environment beyond what can be achieved by the configuration of the default WebSphere Application Server logging infrastructure.

The following example demonstrates how to add a new handler to process requests to the com.myCompany subtree of loggers (see Configuring the logger hierarchy). The main method in this sample gives an example of how to use the newly configured logger.

import java.util.Vector;
import java.util.logging.Filter;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyCustomLogging {

	public MyCustomLogging() {
		super();
	}

	public static void initializeLogging() {
		
		// Get the logger that you want to attach a custom Handler to
		String defaultResourceBundleName = "com.myCompany.Messages";
		Logger logger = Logger.getLogger("com.myCompany", defaultResourceBundleName);
		
		// Set up a custom Handler (see MyCustomHandler example)
		Handler handler = new MyCustomHandler("MyOutputFile.log");
		
		// Set up a custom Filter (see MyCustomFilter example)
		Vector acceptableLevels = new Vector();
		acceptableLevels.add(Level.INFO);
		acceptableLevels.add(Level.SEVERE);
		Filter filter = new MyCustomFilter(acceptableLevels);
		
		// Set up a custom Formatter (see MyCustomFormatter example)
		Formatter formatter = new MyCustomFormatter();

		// Connect the filter and formatter to the handler
		handler.setFilter(filter);
		handler.setFormatter(formatter);
		
		// Connect the handler to the logger
		logger.addHandler(handler);
		
		// avoid sending events logged to com.myCompany showing up in WebSphere
		// Application Server logs
		logger.setUseParentHandlers(false);
				
	}

	public static void main(String[] args) {
		initializeLogging();
		
		Logger logger = Logger.getLogger("com.myCompany");
		
		logger.info("This is a test INFO message");
		logger.warning("This is a test WARNING message");
		logger.logp(Level.SEVERE, "MyCustomLogging", "main", "This is a test SEVERE message");
	}
}
When the above program is run, the output of the program is written to the MyOutputFile.log file. The content of the log is in the expected log file, as controlled by the custom handler, and is formatted as defined by the custom formatter. The warning message is filtered out, as specified by the configuration of the custom filter. The output is as follows:
C:\>type MyOutputFile.log
Sat Sep 04 11:21:19 EDT 2004 INFO This is a test INFO message
Sat Sep 04 11:21:19 EDT 2004 SEVERE This is a test SEVERE message



Related tasks
Configuring the logger hierarchy
Creating log resource bundles and message files
Using a logger
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
Reference topic    

Terms of Use | Feedback

Last updated: Feb 19, 2011 5:25:36 AM CST
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=v610web&product=was-nd-mp&topic=rtrb_cbeusecustom
File name: rtrb_cbeusecustom.html