zookeeper-003-源碼閱讀之日誌輸出配置 log4j

啓動debug 調試zookeeper項目 ,console 管理控制檯將會輸出如下警告信息。 java

log4j:WARN No appenders could be found for logger (org.apache.log4j.jmx.HierarchyDynamicMBean).
log4j:WARN Please initialize the log4j system properly.



爲了輸出日誌方便往後的跟蹤調試,但願可使用log4j將日誌輸出到管理控制檯和日誌文件中。 apache

在源代碼conf 目錄下 ,項目自帶有 log4j.properties 文件 app

program arguments使用 zookeeper 默認配置文件進行啓動 eclipse

E:\zk\zookeeper-3.3.6\conf\zoo_sample.cfg ui

eclipse vm arguments 指定 使用log4j .net

-Dzookeeper.log.dir=file:/E:/zk/log
-Dzookeeper.root.logger=DEBUG,CONSOLE,ROLLINGFILE
-Dlog4j.configuration=file:/E:/zk/zookeeper-3.3.6/conf/log4j.properties debug


其中滾動日誌的輸出目錄能夠經過 log4j.appender.ROLLINGFILE.File=E:/zk/log/zookeeper.log 來配置, 調試

再運行調試,eclipse 管理控制檯將不在打印警告,在 E:/zk/log 目錄能夠找到咱們要的 zookeeper.log 日誌文件。 日誌

#
# ZooKeeper Logging Configuration
#

# Format is "<default threshold> (, <appender>)+

# DEFAULT: console appender only
log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE

# Example with rolling log file
#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE

# Example with rolling log file and tracing
#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE

#
# Log INFO level and above messages to the console
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n

#
# Add ROLLINGFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=DEBUG
log4j.appender.ROLLINGFILE.File=E:/zk/log/zookeeper.log

# Max log file size of 10MB
log4j.appender.ROLLINGFILE.MaxFileSize=10MB
# uncomment the next line to limit number of backup files
#log4j.appender.ROLLINGFILE.MaxBackupIndex=10

log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n


#
# Add TRACEFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.TRACEFILE=org.apache.log4j.FileAppender
log4j.appender.TRACEFILE.Threshold=TRACE
log4j.appender.TRACEFILE.File=zookeeper_trace.log

log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout
### Notice we are including log4j's NDC here (%x)
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L][%x] - %m%n



參考文檔: code

http://fanrey.iteye.com/blog/1683137

http://www.tuicool.com/articles/2MJf6j

http://my.oschina.net/pingpangkuangmo/blog?catalog=3305102

相關文章
相關標籤/搜索