1. 程式人生 > >Log4j指定類的日誌記錄到指定的檔案中

Log4j指定類的日誌記錄到指定的檔案中

LOG4J 配置檔案一段

log4j.rootLogger=ERROR,allerror,heartbeat
log4j.additivity.com.cvicse.inforguard=false

log4j.appender.allerror=org.apache.log4j.RollingFileAppender
log4j.appender.allerror.MaxFileSize=1024KB
log4j.appender.allerror.File=logs/mc.log
log4j.appender.allerror.layout=org.apache.log4j.PatternLayout
log4j.appender.allerror.layout.ConversionPattern=%d [%p] %c <%t> \n%m%n

log4j.logger.com.cvicse.inforguard = DEBUG, allerror
log4j.logger.com.cvicse.inforbroker = ERROR, allerror
log4j.logger.org.springframework = ERROR, allerror
log4j.logger.org.hibernate = ERROR, allerror

#ms heartbeat test
log4j.appender.heartbeat=org.apache.log4j.RollingFileAppender
log4j.appender.heartbeat.MaxFileSize=1024KB
log4j.appender.heartbeat.File=logs/pingHost.log
log4j.appender.heartbeat.layout=org.apache.log4j.PatternLayout
log4j.appender.heartbeat.layout.ConversionPattern=%d [%p] %c <%t> \n%m%n

log4j.logger.com.heartbeat= ERROR,heartbeat

#corba message with MA and SA
log4j.appender.corbamessage=org.apache.log4j.RollingFileAppender
log4j.appender.corbamessage.MaxFileSize=1024KB
log4j.appender.corbamessage.File=logs/message.log
log4j.appender.corbamessage.layout=org.apache.log4j.PatternLayout
log4j.appender.corbamessage.layout.ConversionPattern=%d [%p] %c <%t> \n%m%n

log4j.logger.com.corbamessage= DEBUG,corbamessage

定義allerror 給他指定com.cvicse.inforguard包下面的日誌需要輸出到mc.log檔案中。

使用的時指定類名.class如下程式碼所示:

private static final Log m_log = LogFactory.getLog(GuardMessageHandler.class);

想監控com.cvicse包下面的指定類的日誌到指定檔案中。如上面log4j中所示:定義的corbamessage中指定了一個別名com.corbamessage

配置檔案使用方式:log4j.logger.com.corbamessage=DEBUG,corbamessage

使用時getLog的引數指定在定義時com.corbamessage

private static final Log m_log = LogFactory.getLog("com.corbamessage");

下面程式碼為最普通的log4j的用法。使用時LogFactory.getLog(類名.class);

log4j.appender.guard =org.apache.log4j.RollingFileAppender
log4j.appender.guard.append=true
log4j.appender.guard.Threshold=DEBUG
log4j.appender.guard.bufferedIO=false
log4j.appender.guard.layout=org.apache.log4j.PatternLayout
log4j.appender.guard.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %-32.32C %4L | %m%n
log4j.appender.guard.file=${utmp.base}/logs/utmp.log
log4j.appender.guard.append=true
log4j.appender.guard.MaxBackupIndex=2
log4j.appender.guard.MaxFileSize=50MB

log4j.logger.com.cvicse.inforguard=DEBUG,guard