使用slf4j作为日志系统时,由于slf4j只是一个接口,它需要一个具体实现来执行。
具体参考http://blog.csdn.net/jediael_lu/article/details/43854571
由于slf4j统一了API接口,因此,若log4j实现来日志输出,则只需要设置配置文件的内容即可,log4j.properties中的配置可以分成2类:
第一类定义了日志的输出级别,以及以输出目标(appender)
第二类定义了appender的具体属性。
一、日志输出级别、输出目标(appender)的定义
1、log4j.rootLogger=日志级别,appender1, appender2, ….在后面追加
三、示例:Nutch中的log4j.properties
# Define some default values that can be overridden by system properties hadoop.log.dir=. hadoop.log.file=hadoop.log # RootLogger - DailyRollingFileAppender log4j.rootLogger=INFO,DRFA # Logging Threshold log4j.threshold=ALL #special logging requirements for some commandline tools log4j.logger.org.apache.nutch.crawl.Crawl=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.InjectorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostInjectorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.GeneratorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.DbUpdaterJob=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostDbUpdateJob=INFO,cmdstdout log4j.logger.org.apache.nutch.fetcher.FetcherJob=INFO,cmdstdout log4j.logger.org.apache.nutch.parse.ParserJob=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.IndexingJob=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.DeleteDuplicates=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.CleaningJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.WebTableReader=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostDbReader=INFO,cmdstdout log4j.logger.org.apache.nutch.parse.ParserChecker=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.IndexingFiltersChecker=INFO,cmdstdout log4j.logger.org.apache.nutch.plugin.PluginRepository=WARN log4j.logger.org.apache.nutch.api.NutchServer=INFO,cmdstdout log4j.logger.org.apache.nutch=INFO log4j.logger.org.apache.hadoop=WARN log4j.logger.org.apache.zookeeper=WARN log4j.logger.org.apache.gora=WARN # # Daily Rolling File Appender # log4j.appender.DRFA=org.apache.log4j.DailyRollingFileAppender log4j.appender.DRFA.File=${hadoop.log.dir}/${hadoop.log.file} # Rollver at midnight log4j.appender.DRFA.DatePattern=.yyyy-MM-dd # 30-day backup #log4j.appender.DRFA.MaxBackupIndex=30 log4j.appender.DRFA.layout=org.apache.log4j.PatternLayout # Pattern format: Date LogLevel LoggerName LogMessage log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} - %m%n # Debugging Pattern format: Date LogLevel LoggerName (FileName:MethodName:LineNo) LogMessage #log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n # # stdout # Add *stdout* to rootlogger above if you want to use this # log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n # # plain layout used for commandline tools to output to console # log4j.appender.cmdstdout=org.apache.log4j.ConsoleAppender log4j.appender.cmdstdout.layout=org.apache.log4j.PatternLayout log4j.appender.cmdstdout.layout.ConversionPattern=%m%n
1、指定log4j.rootLogger
log4j.rootLogger=INFO,DRFA
【可选】为单独某个包或者类指定日志级别及appender
#special logging requirements for some commandline tools log4j.logger.org.apache.nutch.crawl.Crawl=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.InjectorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostInjectorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.GeneratorJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.DbUpdaterJob=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostDbUpdateJob=INFO,cmdstdout log4j.logger.org.apache.nutch.fetcher.FetcherJob=INFO,cmdstdout log4j.logger.org.apache.nutch.parse.ParserJob=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.IndexingJob=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.DeleteDuplicates=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.CleaningJob=INFO,cmdstdout log4j.logger.org.apache.nutch.crawl.WebTableReader=INFO,cmdstdout log4j.logger.org.apache.nutch.host.HostDbReader=INFO,cmdstdout log4j.logger.org.apache.nutch.parse.ParserChecker=INFO,cmdstdout log4j.logger.org.apache.nutch.indexer.IndexingFiltersChecker=INFO,cmdstdout log4j.logger.org.apache.nutch.plugin.PluginRepository=WARN log4j.logger.org.apache.nutch.api.NutchServer=INFO,cmdstdout log4j.logger.org.apache.nutch=INFO log4j.logger.org.apache.hadoop=WARN log4j.logger.org.apache.zookeeper=WARN log4j.logger.org.apache.gora=WARN因此,nutch相关的包大多数日志均被重新指定了指向cmdstdout,而其它日志,如hadoop, zookeeper, gora只是重新定义了日志级别,而未重新定义appender,因此它们日志将被指向DRFA。
【可选】指定threshold
# Logging Threshold log4j.threshold=ALL
2、指定某个appender的属性
cmstd:
# # plain layout used for commandline tools to output to console # log4j.appender.cmdstdout=org.apache.log4j.ConsoleAppender log4j.appender.cmdstdout.layout=org.apache.log4j.PatternLayout log4j.appender.cmdstdout.layout.ConversionPattern=%m%n
%m 输出代码中指定的信息,如log(message)中的message
%n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n
DRFA:
# # Daily Rolling File Appender # log4j.appender.DRFA=org.apache.log4j.DailyRollingFileAppender log4j.appender.DRFA.File=${hadoop.log.dir}/${hadoop.log.file} # Rollver at midnight log4j.appender.DRFA.DatePattern=.yyyy-MM-dd # 30-day backup #log4j.appender.DRFA.MaxBackupIndex=30 log4j.appender.DRFA.layout=org.apache.log4j.PatternLayout # Pattern format: Date LogLevel LoggerName LogMessage log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} - %m%n # Debugging Pattern format: Date LogLevel LoggerName (FileName:MethodName:LineNo) LogMessage #log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n
输出文件可以用变量指定,如上面的:
log4j.appender.DRFA.File=${hadoop.log.dir}/${hadoop.log.file}