HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Logging Third-party logging API helps trace and debug problems"
Vol. 5, Issue 11, p. 38

	


Listing 1

log4j.rootCategory=WARN, ROOT_Appender
log4j.category.com.mycompany.employee=ERROR, EMP_Appender
log4j.category.com.mycompany.finance=INFO, FIN_Appender
log4j.additivity.com.mycompany.finance=false

# Set appender specific options.
log4j.appender.EMP_Appender=org.log4j.RollingFileAppender
log4j.appender.EMP_Appender.File=e:\\jdj\\employee.log
log4j.appender.EMP_Appender.Append=true
log4j.appender.EMP_Appender.MaxFileSize=10kb
log4j.appender.EMP_Appender.MaxBackupIndex=2
log4j.appender.EMP_Appender.layout=org.log4j.SimpleLayout

log4j.appender.FIN_Appender=org.log4j.RollingFileAppender
log4j.appender.FIN_Appender.File=e:\\jdj\\finance.log
log4j.appender.FIN_Appender.Append=true
log4j.appender.FIN_Appender.MaxFileSize=10kb
log4j.appender.FIN_Appender.MaxBackupIndex=9
log4j.appender.FIN_Appender.layout=org.log4j.PatternLayout
log4j.appender.FIN_Appender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-15.15t %-8.8p %-30.30c %x - %m\n

log4j.appender.ROOT_Appender=org.log4j.RollingFileAppender
log4j.appender.ROOT_Appender.File=e:\\jdj\\project.log
log4j.appender.ROOT_Appender.Append=true
log4j.appender.ROOT_Appender.MaxFileSize=10kb
log4j.appender.ROOT_Appender.MaxBackupIndex=9
log4j.appender.ROOT_Appender.layout=org.log4j.PatternLayout
log4j.appender.ROOT_Appender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-15.15t %-8.8p %-30.30c %x %C %F %l %L %M - %m\n

Listing 2

import org.log4j.*;

public class LogDemo
{
	public static void main(String args[])
	{
		// Configure the logger using properties file.
		PropertyConfigurator.configure("e:\\jdj\\log.conf");

		// Get the logger for package "com.mycompany.hr"
		// as this is not exclusively configured, we will get
		// a new category inheriting the properties of the ROOT
		// category. The ROOT category is configured to
		// log messages with priority "WARN" or higher. Hence
		// "DEBUG" and "INFO" messages will not be logged.
		Category logger = Category.getInstance("com.mycompany.hr");

		if(logger.isDebugEnabled())
			logger.debug("A debug message");
		if(logger.isInfoEnabled())
			logger.info("A info message");

		logger.warn("A warning message");
		logger.error("An error message");

		// Get the category for package "com.mycompany.finance".
		// This package is configured to log messages with priority
		// "INFO" or higher, hence "DEBUG" messages will not be
		// logged.
		logger = Category.getInstance("com.mycompany.finance");
		if(logger.isDebugEnabled())
			logger.debug("A debug message");
		if(logger.isInfoEnabled())
			logger.info("A info message");

		logger.warn("A warning message");
		logger.error("An error message");

		// Get the category for package "com.mycompany.employee"
		// This package is configured to log messages with priority
		// "ERROR" or higher, hence "DEBUG", "INFO" and "WARN"
		// messages will not be logged. But also note that the
		// additivity flag for this package is true (default setting).
		// Hence the log requests will also go to the root package.
		// So the "ERROR" message will also be logged in the "ROOT"
		// category.
		logger = Category.getInstance("com.mycompany.employee");
		if(logger.isDebugEnabled())
			logger.debug("A debug message");
		if(logger.isInfoEnabled())
			logger.info("A info message");

		logger.warn("A warning message");
		logger.error("An error message");
	}
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.