In many of my projects I implemented log4j as rootLogger instead of categorizing
them based on packages or by business modules
After reading through log4j manual many times, still Ididn't understand how to implement the logger categories and levels. Here is a working sample and troubleshooting in case you run into issues as I did.
## Sample log4j configuration specified in log4.properties
log4j.rootLogger=FATAL, stdout
log4j.logger.com.mycomp.local=WARN, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#Sample code using this logger category
package com.mycomp.local;
import org.apache.log4j.Logger;
public class Sample1
{
private static Logger myLog = Logger.getLogger(Sample1.class);
public Sample1(){}
public static void main(String[] args) {
Enumeration enumList = LogManager.getCurrentLoggers();
while(enumList.hasMoreElements()){
Logger myLog = (Logger)enumList.nextElement();
myLog.getName();
System.out.println("Current Logger -" +myLog.getName() + "="+
myLog.getLevel());
}
myLog.debug("Inside debug1");
myLog.warn("Inside Warn1");
}
The important thing to note about the configuration is to specify appender.
Without the stdout appender in
log4j.logger.com.mycomp.local=WARN, stdout
The program would print DEBUG information even though root category is set to FATAL Give it a try!