简体   繁体   中英

Overwriting log4j not working

I have a log4j properties with the following configuration:

log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.append=false
log4j.appender.LOG.bufferedIO=false

log4j.appender.LOGHISTORY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOGHISTORY.File=${directory}/log/old-logs/app.log
log4j.appender.LOGHISTORY.File.DatePattern='.'yyyy-MM-dd
log4j.appender.LOGHISTORY.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGHISTORY.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOGHISTORY.Threshold=DEBUG
log4j.appender.LOGHISTORY.append=true
log4j.appender.LOGHISTORY.bufferedIO=false

I want to save a history of previous day's logs in the "log/old-logs" folder. This is working great with the DailyRollingFileAppender.

I also want to have a log of the current day in the "log" folder. This is working fine on my localhost (Windows + Eclipse + Geronimo), but is not working properly on my testing server (Linux + WebSphere). In this case the "app.log" is not being overwritten and everything is being appended at the end of the log.

AFAIK Rolling file appender will only roll the file when a specified a max size is reached . The above scenario might be working for you in the local copy each time application is restarted, the log file will overwritten because of .append = false . On a production environment I do not think that the server is restarted that is why the file is written as per .append = false . Could this be your case why overwriting did not work ?

Check this . You should use Append with a capital A.

    log4j.appender.LOG.Append=false 
    log4j.appender.LOGHISTORY.Append=true

For the RollingFileAppender, you need to specify the property "MaxFileSize". This will tell log4j to roll the file over when it reaches that size.

eg: Below will roll over when log file size reaches 2MB

log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=${directory}/log/app.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.layout.ConversionPattern=%d{dd MMM HH:mm:ss,SSS} %-5p [%c{1}] %m%n
log4j.appender.LOG.Threshold=DEBUG
log4j.appender.LOG.MaxFileSize=2MB
log4j.appender.LOG.MaxBackupIndex=2 (This is optional, tells log4j the maximum backup files to take)

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