These two ways in log4j

xiaoxiao2021-03-06  68

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, the log file is generated at a certain time, the configuration files are 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.rapnder.r.file = backup.log log4j.Appender.r.datepattern = '.'yyy-mm-dd log4j.rapnder.r.Layout = Org.apache.log4j.patternLayout log4j.Appender.r.Layout. ConversionPattern =% - D {YYYY-MM-DD HH: mm: SS} [% C] - [% P]% m% N above is configured 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 a month to generate a file can be modified to modify the above configuration: change log4j.Appender.r.datepattern = '.'Yyy-mm-dd to log4j.Appender.R.DatePattern ='. '.'YYYY-mm

Second, automatically generate the new log file profile according to the log file size 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 filelog4j.appender.R.MaxBackupIndex = 1log4j.appender.R.layout = org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern =% - d {yyyy-MM-dd HH: mm: ss } [% C] - [% P]% m% N

Where: # log file size log4j.Appender.r.maxfilesize = 100kb # Save a backup file log4j.Appender.r.maxbackupindex = 1

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

New Post(0)