配置log4j以进行maxsize和rotation [英] Configure log4j for maxsize and rotation
问题描述
我在我的Java应用程序中使用log4j。
我想配置maxsize(最大值为1Mb)和15天后自动删除。
I am using log4j in my Java application. I want to config both of maxsize (max is 1Mb) and auto delete after 15 days.
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=15
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
但似乎在log4j中无法同时配置2个我的要求,对?
But it seems that in log4j can not config both of 2 my requirements, right?
我怎么做?
谢谢大家。
How i can do it? Thank all.
推荐答案
您可以为文件删除目的创建一个批处理文件...批处理文件如下:
You can create a batch file for the file deletion purpose ... the contents of the batch file will be as follows
cd "C:\<folder-location>\
del *.log
你可以创建一个调度程序条目来每15天运行一次这个文件......这样它将在15天后删除所有日志文件。(此外,使用批处理文件,您还可以创建一个日志文件的zip作为备份,并在其他位置保持安全...这是你根本无法使用log4j做的事情)
You can create a scheduler entry to run this file every 15 days... this way it will delete all log files after 15 days. (moreover using batch file you can also create a zip of log file as backup and keep it safe in some other location ... this is something you cannot do using log4j at all )
将文件保存为zip备份(使用7-zip)
TO keep files as a zip backup (using 7-zip)
C:\Program Files\7-Zip\7z.exe a -tzip C:\B\ZipFile.zip C:\A\*.*
这会将文件夹A中的所有文件复制到一个zip文件中。
This will copy all files from folder A to B in a zip file.
保持maxsize是你已经在log4j配置文件中完成了。
Keeping maxsize is already done by you in the log4j configuration file.
这篇关于配置log4j以进行maxsize和rotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!