简体   繁体   中英

Log4j appender size

My appender size was set to MB in log4j appneder but when I changed it to KB's It did not work. Can I set log4j appender size in KB's.

Like

log4j.appender.processAppender.MaxFileSize=20KB

If yes then is there need to add another property? My file looks like:

log4j.rootLogger=DEBUG,FileAppender
log4j.logger.process=debug, processAppender
log4j.appender.processAppender=org.apache.log4j.RollingFileAppender 
log4j.appender.processAppender.File=D:/logs/process.log

log4j.appender.processAppender.Append=true
log4j.appender.processAppender.MaxFileSize=20KB
log4j.appender.processAppender.MaxBackupIndex=3
log4j.appender.processAppender.layout=org.apache.log4j.PatternLayout

log4j.appender.processAppender.layout.ConversionPattern=%d %-5p - %m%n

I've used below code

    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=AgentWorkstation.log

    log4j.appender.file.Append=true
    log4j.appender.A1.MaxFileSize=10KB

    log4j.appender.A2.MaxBackupIndex=3

    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS}
    %-5p (%C{1}:%L) - %m\n

It worked perfectly for me

Set the maximum size that the output file is allowed to reach before being rolled over to backup files. In configuration files, the MaxFileSize option takes an long integer in the range 0 - 2^63. You can specify the value with the suffixes "KB", "MB" or "GB" so that the integer is interpreted being expressed respectively in kilobytes, megabytes or gigabytes. For example, the value "10KB" will be interpreted as 10240.

Reference

Yes you can set KB as a unit and your configuration seems like correct. This is the explanation from the IBM's webpage .

Specifies the maximum file size that the log file is allowed to take up on disk.

Default size is 100 MB.

Uses the following format:

log4j.appender.S1.MaxFileSize=SizeUnits

where:

Size is the numeric value for the file size. Units can be KB, MB, or GB. For example, to limit the log file size to 10 MB, enter the following:

log4j.appender.S1.MaxFileSize=10 MB

If the MaxBackupIndex parameter is set to 1 or greater, then the logging process automatically creates a backup file when the log file reaches the MaxFileSize. The total number of backup files is determined by MaxBackupIndex option.

You should change 3 property under the Appender.

    log4j.appender.CARBON_LOGFILE.Append=true
    log4j.appender.CARBON_LOGFILE.MaxFileSize=10MB
    log4j.appender.CARBON_LOGFILE.MaxBackupIndex=20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM