说我有一个这样的班级:
public class MyClass { private Logger log = LoggerFactory.getLogger(MyClass.class); //org.slf4j.LoggerFactory public void foo(Params p) { log.info("Foo params: " + p); long t1 = System.currentTimeMillis(); Result r = someMethod(p); long t2 = System.currentTimeMillis(); log.info("Foo result: " + r) log.info("Foo time taken: + (t2-t1)/1000); } }
现在,当涉及到打印此信息时,我希望能够打开和关闭不同类型的信息(参数,结果,花费的时间)。
使用日志记录级别进行区分的问题在于,更精细的粒度级别还包含了更粗糙的级别。
如何轻松设置?
您可以通过创建自定义日志记录类来设置不同的日志记录行为。
这是我的解决方案:
public abstract class AbstractLogger { protected Logger log; protected Class callingClass; public AbstractLogger(Class c) { this.log = LoggerFactory.getLogger(this.getClass()); this.callingClass = c; } public void log(String s) { log.debug(this.callingClass + " :" + s); } } public class LoggerOne extends AbstractLogger { public LoggerOne(Class c) { super(c); } } public class LoggerTwo extends AbstractLogger { public LoggerTwo(Class c) { super(c); } } public class LoggerThree extends AbstractLogger { public LoggerThree(Class c) { super(c); } }
在Log4j.properties中设置
#Define custom levels by package #set to ERROR to turn them off log4j.logger.org.myproject.loggers.LoggerOne=ERROR log4j.logger.org.myproject.loggers.LoggerTwo=DEBUG log4j.logger.org.myproject.loggers.LoggerThree=DEBUG
使用这些记录器时:
要使用这些记录器:
public class MyMain { // private Logger log = LoggerFactory.getLogger(MyMain.class); private AbstractLogger l1= new LoggerOne(this.getClass()); private AbstractLogger l2= new LoggerTwo(this.getClass()); private AbstractLogger l3= new LoggerThree(this.getClass()); public void run() { l1.log("log 1"); long t1 = System.currentTimeMillis(); try { Thread.sleep(1000); //1000 milliseconds is one second. } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } long t2 = System.currentTimeMillis(); l2.log("log 2"); l3.log("Foo time taken:" + (t2-t1)/1000); } public static void main(String[] args) { MyMain me = new MyMain(); me.run(); } }
日志输出:
12:27:29 DEBUG LoggerTwo:18 - class maventestspace.MyMain :log 2 12:27:29 DEBUG LoggerThree:18 - class maventestspace.MyMain :Foo time taken:1
请注意,LoggerOne无法打印,因为在属性文件中将其设置为ERROR。
要重定向这些单独的日志文件,您需要设置新的附加程序和记录器。
log4j.logger.org.myproject.loggers.LoggerOne=DEBUG, file1, stdout log4j.logger.org.myproject.loggers.LoggerTwo=DEBUG, file2, stdout log4j.logger.org.myproject.loggers.LoggerThree=DEBUG, file3, stdout # Direct log messages to a log file log4j.appender.file1=org.apache.log4j.RollingFileAppender #log4j.appender.TextProcessor.Threshold=debug log4j.appender.file1.File=E:\\logs\\log1.txt log4j.appender.file1.MaxFileSize=10MB log4j.appender.file1.MaxBackupIndex=1 log4j.appender.file1.layout=org.apache.log4j.PatternLayout log4j.appender.file1.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to a log file log4j.appender.file2=org.apache.log4j.RollingFileAppender #log4j.appender.TextProcessor.Threshold=debug log4j.appender.file2.File=E:\\logs\\log2.txt log4j.appender.file2.MaxFileSize=10MB log4j.appender.file2.MaxBackupIndex=1 log4j.appender.file2.layout=org.apache.log4j.PatternLayout log4j.appender.file2.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n # Direct log messages to a log file log4j.appender.file3=org.apache.log4j.RollingFileAppender #log4j.appender.TextProcessor.Threshold=debug log4j.appender.file3.File=E:\\logs\\log3.txt log4j.appender.file3.MaxFileSize=10MB log4j.appender.file3.MaxBackupIndex=1 log4j.appender.file3.layout=org.apache.log4j.PatternLayout log4j.appender.file3.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1}:%L - %m%n