log4j的强大功能无可置疑,但实际应用中免不了遇到某个功能需要输出独立的日志文件的情况,怎样才能把所需的内容从原有日志中分离,形成单独的日志文件呢?其实只要在现有的log4j基础上稍加配置即可轻松实现这一功能。
<span style="font-size:24px;">log4j.rootLogger=info,logfile
# 设置dao,service类的日志
log4j.logger.com.dong.dao=ERROR,myDao
log4j.logger.com.dong.service= ERROR,myService
log4j.appender.logfile=org.apache.log4j.ConsoleAppender #控制台输出日志
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%m%n
# service的设置
log4j.appender.myService=org.apache.log4j.RollingFileAppender #按天记录日志
log4j.appender.myService.File=/usr/tmp/logs/service.log
log4j.appender.myService.MaxFileSize=50000KB
log4j.appender.myService.MaxBackupIndex=1
log4j.appender.myService.layout=org.apache.log4j.HTMLLayout
log4j.additivity.loopPriceCalendarEntry =false #com.dong.service类的日志只输入到service.log文件
log4j.appender.myDao=org.apache.log4j.RollingFileAppender
log4j.appender.myDao.File=/usr/tmp/logs/dao.log
log4j.appender.myDao.MaxFileSize=50000KB
log4j.appender.myDao.MaxBackupIndex=1
log4j.appender.myDao.layout=org.apache.log4j.PatternLayout
log4j.appender.myDao.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}%5p[%t](%F:%L)-%m%n</span>