package daveutil;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Log4jWrapper {
/*
* TRACE, DEBUG, INFO, WARN, ERROR, FATAL
*/
Level level = Level.DEBUG;
public static Log4jWrapper LOG;
private Logger logger;
static {
synchronized(Log4jWrapper.class){
if(LOG == null){
LOG = new Log4jWrapper("daveapp");
}
}
}
private Log4jWrapper(String logger) {
this.logger = Logger.getLogger(logger);
}
public void log(Object o) {
logger.log(level, o);
}
public void log(Object o,Throwable e) {
logger.log(level, o, e);
}
public void log(String callerFCQN,Object o,Throwable e) {
logger.log(callerFCQN,level, o, e);
}
public void trace(Object o) {
logger.trace(o);
}
public void trace(Object o,Throwable e) {
logger.trace(o,e);
}
public void debug(Object o) {
logger.debug(o);
}
public void debug(Object o,Throwable e) {
logger.debug(o,e);
}
public void info(Object o) {
logger.info(o);
}
public void info(Object o,Throwable e) {
logger.info(o,e);
}
public void warn(Object o) {
logger.warn(o);
}
public void warn(Object o,Throwable e) {
logger.warn(o,e);
}
public void error(Object o) {
logger.error(o);
}
public void error(Object o,Throwable e) {
logger.error(o,e);
}
public void fatal(Object o) {
logger.fatal(o);
}
public void fatal(Object o,Throwable e) {
logger.fatal(o,e);
}
}
Using log4j wrapper
package daveapp;
import daveutil.Log4jWrapper;
public class TestDaveApp {
public void testDaveApp(){
System.out.println("Use Log4jWrapper");
Log4jWrapper.LOG.info("TestDaveApp info");
Log4jWrapper.LOG.info("TestDaveApp info",new Exception("TestDaveApp"));
}
}
log4j properties configuration
Pattern layout
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
Property Configurator
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html
All option values admit variable substitution. The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself.
#log4j.rootLogger=debug, stdout, dave, daveutil
log4j.logger.dave=DEBUG,stdout, dave
log4j.logger.daveutil=DEBUG, stdout, daveutil
log4j.logger.daveapp=DEBUG, stdout, daveapp
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
log4j.appender.dave=org.apache.log4j.RollingFileAppender
# do not send to other logs
log4j.additivity.dave=false
log4j.appender.dave.File=${weblogic.Name}.dave.log
log4j.appender.dave.MaxFileSize=10KB
# Keep one backup file
log4j.appender.dave.MaxBackupIndex=5
log4j.appender.dave.layout=org.apache.log4j.PatternLayout
log4j.appender.dave.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.daveutil=org.apache.log4j.RollingFileAppender
# do not send to other logs
log4j.additivity.daveutil=false
log4j.appender.daveutil.File=${weblogic.Name}.daveutil.log
log4j.appender.daveutil.MaxFileSize=10KB
# Keep one backup file
log4j.appender.daveutil.MaxBackupIndex=5
log4j.appender.daveutil.layout=org.apache.log4j.PatternLayout
log4j.appender.daveutil.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.daveapp=org.apache.log4j.RollingFileAppender
# do not send to other logs
log4j.additivity.daveapp=false
log4j.appender.daveapp.File=${weblogic.Name}.daveapp.log
log4j.appender.daveapp.MaxFileSize=10KB
# Keep one backup file
log4j.appender.daveapp.MaxBackupIndex=5
log4j.appender.daveapp.layout=org.apache.log4j.PatternLayout
log4j.appender.daveapp.layout.ConversionPattern=%p %t %c - %m%n
Using FCQN - fully qualified class name
Log4jWrapper.LOG.log(TestDaveApp.class.getName(),"TestDaveApp info",new Exception("TestDaveApp"));
INFO (Log4jWrapper.java:62) - TestDaveApp info
DEBUG (TestDave.java:22) - TestDaveApp info
Thanks Dave for your article. I have question how to use LocationAware in log4j wrapper. The reason is I want to know caller class and line no when class method called. Thanks in advace
ReplyDelete