1.log4j打印完整的异常信息需要使用异常提供的log.error("异常",e);而不能写成log.error("异常"+e),后者只会把异常名打出来,不会打堆栈信息.
2.不要混淆logger与appender
3.rootLogger充当父Logger的角色, logger为子Logger的角色.
rootLogger怎么理解:
这个机制意思很简单。就是类似于Java package一样,比如我们的一个包:cn.lsw.base.log4j2。而且,可以发现我们前面生成Logger对象的时候,命名都是通过 Hello.class.getName(); 没有在配置文件中配置logger,怎么能有这个logger呢?
a.因为有所谓的Logger 继承的问题。比如 如果你给cn.lsw.base定义了一个logger,那么他也适用于cn.lsw.base.lgo4j2这个logger。名称的继承是通过点(.)分隔的。
b.在log4j2.xml里面的Loggers标签里有一个子节点不是logger而是root,而且这个root没有name属性。这个root相当于根节点。
c.你所有的logger都适用与这个logger,所以,即使你在很多类里面通过 类名.class.getName() 得到很多的logger,而且没有在配置文件的loggers下面做配置,他们也都能够输出,因为他们都继承了root的log配置。
注: 以上适合log4j2,也适合lo4j
4.关于一份日志同时位于多个appender输出里: additivity.
记录日志的粒度,比如如果想将一些类里某个方法的信息单独打到一个日志文件里.可以单独定义一个logger而不使用rootLogger.
private static Logger partnerLogger = LoggerFactory
.getLogger("com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService.partner");//应用程序中按照配置中相应的串取logger取可。
log4j.rootLogger=INFO, if
log4j.logger.com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService.partner=INFO,partner
log4j.appender.partner=org.apache.log4j.DailyRollingFileAppender
log4j.appender.partner.File=${system.logPath}/partner.log
log4j.appender.partner.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.partner.layout=org.apache.log4j.PatternLayout
log4j.appender.partner.layout.ConversionPattern=%-5p %d{yyyy-MM-dd-HH HH:mm:ssS} %c:%L - %m%n
注意,在子Logger里打出的INFO, 也会在父Logger对应的appender上输出. 如果只想在子Logger里面看日志,可以再添加一句
log4j.additivity.com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService.partner=false
这样该方法的日志只会出现在对应的partner logger对应的appender里, 而不会出现在rootLogger对应的输出上.
这是解决了一份信息在父子Logger里同时出现的情况, 遇到的另一种情况是在一个Logger里, 重复出现同样的日志.
5.一个appender输出的日志,重复记录:
比如下面的sql被记录二次:
DEBUG 2012-03-05
12:46:32249 net.sourceforge.jdbclogger.core.PreparedStatementWrapper:156
- Prepared Statement : select * from t_tourism_post_watch limit 0,10000
DEBUG
2012-03-05 12:46:32249
net.sourceforge.jdbclogger.core.PreparedStatementWrapper:156 - Prepared
Statement : select * from t_tourism_post_watch limit 0,10000
而导致重复记录的配置肯定是配置了多个logger,指向了一个appender, 即配置错误.
log4j.rootLogger=INFO,stdout,de
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.de=org.apache.log4j.DailyRollingFileAppender
log4j.appender.de.File=debug.log
log4j.appender.de.layout=org.apache.log4j.PatternLayout
log4j.appender.de.layout.ConversionPattern= %-5p %d{yyyy-MM-dd HH:mm:ssS} %c:%L - %m%n
log4j.appender.de.append = true
log4j.logger.net.sourceforge.jdbclogger=DEBUG,de
------------------------------------------------
解决:
因为rootLogger会将debug信息同时写入de对应的appender,而另一个logger:net.sourceforge.jdbclogger也会写入de里。这样就会有二个logger同时往一个appender里面写。肯定会重复。只需去掉rootlogger的de appender
5. 一个logger可以对应多个appender,通常是不同性质的appender比如控制台与文件.
log4j.rootLogger=DEBUG,wn,if,formonitor,notice(表示会把所有级别在debug之上的信息输出到wn,if,formonitor,notice这些appender上)
6.通常会定义一个大而全的调试logger去读所有信息
log4j.appender.if=org.apache.log4j.DailyRollingFileAppender
log4j.appender.if.File=${system.logPath}/info.log
log4j.appender.if.Threshold = INFO
log4j.appender.if.layout=org.apache.log4j.PatternLayout
log4j.appender.if.layout.ConversionPattern= %-5p %d{yyyy-MM-dd HH:mm:ssS} %c:%L - %m%n
log4j.appender.if.append = true
----------------------------------------------------------------------------------
#定义logger部分
log4j.logger.com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService=info,partner
log4j.additivity.com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService = false
//定义appender部分.
log4j.appender.partner=org.apache.log4j.DailyRollingFileAppender
log4j.appender.partner.File=${system.logPath}/partner.log
log4j.appender.partner.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.partner.layout=org.apache.log4j.PatternLayout
log4j.appender.partner.layout.ConversionPattern=%-5p %d{yyyy-MM-dd-HH HH:mm:ssS} %c:%L - %m%n
log4j.appender.partner.Threshold = info
注:
1).log4j.appender.partner.Threshold = info与log4j.logger.com.sogou.adm.bizdev.daemon.multiidea.autoauditad.service.MultiIdeaAutoAuditService=info,partner的区别:
log4j会先根据Threshold过滤,再根据logger的level过滤写入日志。
2).Threshold= info会按照优先级别error>warn>info>debug将所有级别大于info的日志打出来
The status logger is used internally by log4j2 components. Setting status="debug"
(or "trace") in the configuration will cause this internal logging to be output to the command line.
It will print debug information about which log4j2 plugin components are loaded (all configuration elements map to log4j2 plugins), and more details like for example what appenders and loggers were found, what parameters they have and how they are combined.
This is useful for troubleshooting configuration issues.
Configuration with XML
The configuration element in the XML file accepts several attributes:
Attribute Name | Description |
---|---|
advertiser | (Optional) The Advertiser plugin name which will be used to advertise individual FileAppender or SocketAppender configurations. The only Advertiser plugin provided is 'multicastdns". |
dest | Either "err", which will send output to stderr, or a file path or URL. |
monitorInterval | The minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes. |
name | The name of the configuration. |
packages | A comma separated list of package names to search for plugins. Plugins are only loaded once per classloader so changing this value may not have any effect upon reconfiguration. |
schema | Identifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema validation will take place. |
shutdownHook | Specifies whether or not Log4j should automatically shutdown when the JVM shuts down. The shutdown hook is enabled by default but may be disabled by setting this attribute to "disable" |
status | The level of internal Log4j events that should be logged to the console. Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal". Log4j will log details about initialization, rollover and other internal actions to the status logger. Setting status="trace" is one of the first tools available to you if you need to troubleshoot log4j. |
strict | Enables the use of the strict XML format. Not supported in JSON configurations. |
verbose | Enables diagnostic information while loading plugins. |