Log log files are often used in development, which may not produce very large log files for maintenance and [troubleshooting]. So we want to be able to generate a log file every day or month so that the file is not too large. or according to the log file size to determine, more than the specified size, the log automatically add new files.
The following implementations of the two methods in log4j are simple, as long as they are set in the configuration file. Examples are as follows:
The first way:
Log4j.rootlogger = Debug,con,file
Log4j.appender.con=org.apache.log4j.consoleappender
Log4j.appender.con.threshold=debug
Log4j.appender.con.target=system.out
Log4j.appender.con.layout=org.apache.log4j.patternlayout
LOG4J.APPENDER.CON.LAYOUT.CONVERSIONPATTERN=[LOG4J]%d%f:line#%l-%c-%-4r [%t]%-5p%x%n%m%n%n
#Daily Log
Log4j.appender.file=org.apache.log4j.dailyrollingfileappender
Log4j.appender.file.file=northeasttycoonlogs/log.txt
Log4j.appender.file.append=true
Log4j.appender.file.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c%x-%m%n
The above configuration is to produce a backup file every day. Where the backup file path and name is: Northeasttycoonlogs/log.txt
The second automatically generates new log files based on the size of the log file
The contents of the configuration file are as follows:
Log4j.rootlogger=error,r
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.appender.r.layout.conversionpattern=%-d{yyyy-mm-dd HH:MM:SS} [%c]-[%p]%m%n
which
#日志文件的大小
log4j.appender.r.maxfilesize=100kb
# Save a backup file
Log4j.appender.r.maxbackupindex=1
Java log4j log file