log4j create a new log file every day

本文介绍如何使用Log4j配置文件来实现不覆盖的日志记录功能。通过设置滚动策略和文件名模式,每天会生成一个新的日志文件。此外,还介绍了如何将log4j.jar添加到项目中并配置相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

After I joined in new project, I realized log is very important. We can log in console or file. Here is to introduce a way to log in file without overwrite. It's really easy to use log4j.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.rolling.RollingFileAppender
# Set the name of the file
log4j.appender.FILE.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.FILE.rollingPolicy.FileNamePattern = tweetError.%d{MM-dd-yyyy}.log
#log4j.appender.FILE.File=tweeterror.log

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=info

# Set the append to false, overwrite
log4j.appender.FILE.Append=true

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

What you need to do is to copy the code in a log4j.propeties file and add log4j.jar into your project. Then you'll see it'll create a new file every day when error is logged.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值