Java log4j log file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.