Java中怎么将log4j日志文件导出为一个txt的文件?

2024-12-16 07:49:35
推荐回答(3个)
回答1:

log4j.properties的基本配置如下

### set log levels ###
log4j.rootLogger = debug , D , E

### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log //保存成你想要的文件txt

log4j.appender.D.DatePattern = '.'yyyy-MM-dd
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
## 输出DEBUG级别以上的日志
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
## 只输出ERROR级别以上的日志!!!
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

回答2:

log4j.appender.logFile.File=${catalina.base}/logs/1.log

回答3:

log4j.appender.stuout=org.apache.log4j.FileAppender