How to control log file with daily rolling and max file size by log4j?

后端 未结 3 1412
情书的邮戳
情书的邮戳 2021-02-04 08:40

I would like to create log file that can be rolled at the beginning of the next day or if it\'s reached to specified file size and log file must be contained inside date folder.

3条回答
  •  清酒与你
    2021-02-04 09:26

    Daily works for me with xml in question only transformed into log4j.properties equivalent to roll over after 100KB (for testing purposes):

    # Root logger option
    log4j.rootLogger=INFO, file
    
    # Direct log messages to a file
    log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
    log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
    log4j.appender.file.RollingPolicy.FileNamePattern=/path/to/logs/%d{yyyyMMdd}/myLog_%d{yyyyMMddHH}.log
    log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
    log4j.appender.file.TriggeringPolicy.maxFileSize=100000
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    log4j.appender.file.Append=true
    

    This will create a timestamped directory (one directory per day - optional) with logs with the following format (one per hour for testing purposes - change FileNamePattern to suit your needs):

    myCompanyLog_201602031030.log

    myCompanyLog_201602031130.log

提交回复
热议问题