可以将控制台输出信息保存到本地的Log4j配置

时间:2022-03-24 21:47:59

专注java已6年,欢迎加入java核心技术QQ群:135138817,每周五晚有群主进行技术讲座。  

在生产环境上,我们都需要将日志保持在文件中。下面的log4j会按天,每天保存一个,如果当天的第一个超过了一定的尺寸会再重新保存一个。debug信息会保存在debug.log里,错误日志会保存在error.log中。

  上代码,呵呵:

  

#root directory for log files
dir=F://Logs
#log file for online
file00=error.log
#log file for batch
file01=debug.log
#log file size
fileSize=10000KB
#back up numbers for log file
backup=10

#################################################################
#### Use two appenders, one to log to console, another to log to a file
log4j.category.errorMsg=ERROR, ree00,errorMsgFile
log4j.category.debugMsg=DEBUG, stdout,debugMsgFile
log4j.rootLogger=warn,ree00,stdout
log4j.appender.ree00=org.apache.log4j.RollingFileAppender
log4j.appender.ree00.File=${dir}/${file00}
log4j.appender.ree00.MaxFileSize=${fileSize}
log4j.appender.ree00.MaxBackupIndex=${backup}
log4j.appender.ree00.layout=org.apache.log4j.PatternLayout
log4j.appender.ree00.layout.ConversionPattern=[%d{yyy-MM-dd HH:mm:ss;SSS},%6.6r]%-5p%x(%F:%L) - %m%n
# Print only messages of priority WARN or higher for your category
#log4j.category.your.category.name=WARN
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p]%l - %m%n
#### Second appender writes to a file for ERROR
log4j.appender.errorMsgFile=org.apache.log4j.RollingFileAppender
log4j.appender.errorMsgFile.File=${dir}/${file00}
log4j.appender.errorMsgFile.MaxFileSize=${fileSize}
log4j.appender.errorMsgFile.MaxBackupIndex=${backup}
log4j.appender.errorMsgFile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorMsgFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}    [%t] - %c%n%p:%m %n    at %l%n%n
#### appender writes to a log file    for DEBUG
log4j.appender.debugMsgFile=org.apache.log4j.RollingFileAppender
log4j.appender.debugMsgFile.File=${dir}/${file01}
log4j.appender.debugMsgFile.MaxFileSize=${fileSize}
log4j.appender.debugMsgFile.MaxBackupIndex=${backup}
log4j.appender.debugMsgFile.layout=org.apache.log4j.PatternLayout
log4j.appender.debugMsgFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}    [%t] - %c%n%p:%m %n    at %l%n%n

日志将会保存到f://Logs文件夹下,请先创建这个文件夹。

欢迎大家拍砖!嘿嘿。