今天看到一篇文章,谈Log4j对系统的效能的影响...整理如下:
一般,我们用log4j写log的时候,很习惯于如下面记录:
原文请参考这里: Sneaky, sneaky Log4J
===========================================================================
补遗篇~~~~~!
log4j.rootLogger=info, Console
log4j.logger.net.csdn.blog.xport=debug, RollingFile
log4j.additivity.net.csdn.blog.xport=false
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%t] %C{1} - %m%n
log4j.appender.RollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.RollingFile.File=SystemLog.log
log4j.appender.RollingFile.Append=true
log4j.appender.RollingFile.MaxFileSize=2MB
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d %-5p [%t] %C{1} - %m%n
一般,我们用log4j写log的时候,很习惯于如下面记录:
2005-01-11 09:57:57,816 DEBUG (BizProc.java:275) Begin to get connection... 2005-01-11 09:57:57,816 DEBUG (BizProc.java:275) Obtain connection 2345@defd 2005-01-11 09:57:57,816 DEBUG (BizProc.java:275) Begin transaction [TX_2015_RECV] processing... 2005-01-11 09:57:57,925 DEBUG (BizProc.java:328) All of the values were: |
这里会把Java Source Code中的行号带出来,查找错误很方便...
但,这也是问题之所在...文章中介绍,Log4j之所以可以得到行号,是用了JVM的一个特性,不知道大家有没有注意到,如果我们的Java程序在运行时出现错误,会出现下面的信息:
javax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:189) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301) at org.apache.naming.NamingContext.lookup(NamingContext.java:834) at org.apache.naming.NamingContext.lookup(NamingContext.java:181) at org.apache.naming.NamingContext.lookup(NamingContext.java:822) at org.apache.naming.NamingContext.lookup(NamingContext.java:181) at org.apache.naming.NamingContext.lookup(NamingContext.java:822) at org.apache.naming.NamingContext.lookup(NamingContext.java:181) at org.apache.naming.NamingContext.lookup(NamingContext.java:822) at org.apache.naming.NamingContext.lookup(NamingContext.java:194) at org.apache.naming.SelectorContext.lookup(SelectorContext.java:183) at javax.naming.InitialContext.lookup(InitialContext.java:347) |
看到黑体字没有,原来JVM已经内嵌支持Debug出行号信息了!可是,这里是出错的时候,JVM抛出的,那为什么用 log4j.info() 的时候,也可以看到行号呢?
下面就是重点,Log4j中记录行号的过程...
(1) 调用了 log4j.info() 或是log4j.debug()方法... |
但是,请注意,建立运行时的堆栈跟踪,对JVM来说,是一项非常大的开销!
所以,我们在配置log4j的PatternLayout的时候,如果是要上线到正式环境,一定要记者把参数关掉,
一般来说,用 “%d %-5p %m%n" 就可以满足了!
原文请参考这里: Sneaky, sneaky Log4J
===========================================================================
补遗篇~~~~~!
log4j.rootLogger=info, Console
log4j.logger.net.csdn.blog.xport=debug, RollingFile
log4j.additivity.net.csdn.blog.xport=false
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p [%t] %C{1} - %m%n
log4j.appender.RollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.RollingFile.File=SystemLog.log
log4j.appender.RollingFile.Append=true
log4j.appender.RollingFile.MaxFileSize=2MB
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=%d %-5p [%t] %C{1} - %m%n