Log4j Use: Automatically set backup files

zhaozj2021-02-16  85

The B / S structural system we developed, which generally has a daily run log record. When saving a log file form, you will often encounter a problem: the log file is too large. It is also a problem for the online log information for receiving log information. So I hope to produce a log file every day or every month, so the file is not too big. Or determine based on the log file size, exceeding the specified size, the log is automatically added to the new file.

The implementation of these two ways in log4j is very simple, as long as it is set in the configuration file.

First, generate log files at a certain time, the configuration file is as follows:

# Set root logger level to error and its Only appender to a1.

Log4j.rootlogger = error, r

# R IS set to be a dailyrollingfileappender.

Log4j.Appender.r = org.apache.log4j.dailyRollingFileAppender

Log4j.Appender.r.file = backup.log

Log4j.Appender.r.datepattern = '.'yyy-mm-dd

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.rapnder.r.Layout.ConversionPattern =% - D {yyyy-mm-dd hh: mm: ss} [% C] - [% P]% m% N

The above configuration is to generate a backup file every day. The name of the backup file is Backup.log.

The specific effect is this: The log information on the same day is recorded in the backup.log file, the previous day record in the file named backup.log.log.yleyy-mm-dd.

Similarly, if you need a file per month to modify the above configuration:

will

Log4j.Appender.r.datepattern = '.'yyy-mm-dd

Change to

Log4j.Appender.r.datepattern = '.'yyyy-mm

Second, automatically generate a new log file according to the log file size

The configuration file content is as follows:

# Set root logger level to error and its Only appender to a1.

Log4j.rootlogger = error, r

# R is set to be a rollingfileappender.

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = backup.log

# log4j.appender.r.maxfilesize = 100kb

# Keep One Backup File

Log4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.rapnder.r.Layout.ConversionPattern =% - D {yyyy-mm-dd hh: mm: ss} [% C] - [% P]% m% N

among them:

#

Log file size

Log4j.Appender.r.maxfilesize = 100kb #

Save a backup file

Log4j.Appender.r.maxbackupindex = 1

转载请注明原文地址:https://www.9cbs.com/read-12220.html

New Post(0)