log4j配置文件及nutch中的日志配置
使用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, ….示例:log4j.rootLogger=INFO,stdout,DRFA
(1)设置整个工程的日志默认级别,以及日志的输出目标
(2)日志级别为ALL<DEBUG<INFO<WARN<ERROR<FATAL<OFF,不区分大小写。示例中的日志级别为INFO,即只有大于或者等于INFO的日志才会被记录。
(3)日志的输出目标有以下几类:
- ConsoleAppender(控制台)
- FileAppender(文件)
- DailyRollingFileAppender(每天产生一个日志文件
- RollingFileAppender(文件大小到达指定尺寸时产生一个新的文件)
- WriteAppender(将日志信息以流格式发送到任意指定的地方)
- JDBCAppender(将日志信息保存到数据库中)
2、log4j.logger.org.mypackage[.MyClass]=日志级别,appender1, appender2, ….
示例:log4j.logger.org.apache.nutch.crawl.GeneratorJob=INFO,cmdstdout
(1)log4j.rootLogger 中的设置对整个工程生效,若只想对某个包或者某个类生效,可以使用本配置项。
3、log4j.threshold=ALL
In fact , threshold's level should be larger or equals rootlogger's level.
usually, thresold can be used to define some special appender's filter log level.
eg:
there are two appenders: console and file,and the level is warn, if you just only want the error message be written to file, so you can use "file.threshold=error" to filter out the "warn" message.
二、具体某个appender的设置
1、log4j.appender.appender1.File=文件目录及文件
2、log4j.appender.appender1.MaxFileSize=最大文件大小
3、log4j.appender.appender1.MaxBackupIndex=备份文件个数
其中,appender1是在log4j.rootlogger或者具体包日志中定义过的;
文件目录及文件,例如,/home/admin/logs/hutudan.log
最大文件大小,例如,100KB
备份文件个数,例如,1
4、log4j.appender.ServerDailyRollingFile.DatePattern=日志后缀格式
例如,'.'yyyy-MM-dd
5、log4j.appender.appender1.layout=org.apache.log4j.日志布局格式
HTMLLayout(以HTML表格形式布局)
SimpleLayout(包含日志信息的级别和信息字符串)
TTCCLayout(包含日志产生的时间,执行绪,类别等信息)
PatternLayout(可以灵活的指定布局格式,常用)
6、log4j.appender.appender1.layout.ConversionPattern=日志输出格式
例如,%d - %m%n或%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n
%c 输出日志信息所属的类的全名
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-M-dd HH:mm:ss },输出类似:2002-10-18- 22:10:28
%f 输出日志信息所属的类的类名
%l 输出日志事件的发生位置,即输出日志信息的语句处于它所在的类的第几行
%m 输出代码中指定的信息,如log(message)中的message
%n 输出一个回车换行符,Windows平台为“rn”,Unix平台为“n”
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。如果是调用debug()输出的,则为DEBUG,依此类推
%r 输出自应用启动到输出该日志信息所耗费的毫秒数
%t 输出产生该日志事件的线程名
可参考:http://blog.sina.com.cn/s/blog_4e4dd5570100qowy.html
7、log4j.appender.ServerDailyRollingFile.Append=true
在后面追加
三、示例: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}