gpt4 book ai didi

java - Log4J 2配置: How to split log files by size and by day at the same time?

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:02 25 4
gpt4 key购买 nike

我需要每天创建一次新的日志文件,或者如果日志文件大小变为 250 MB。那么我如何通过 log4j.properties 文件来做到这一点?按天划分是有效的,但如果大小超过 250MB,则不会发生任何情况。我当前的属性文件是:

 # Root logger option
log4j.rootLogger=INFO,console, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.encoding=UTF-8
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000
log4j.appender.rollingFile.MaxFileSize=10MB
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

# configuration to print on console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

最佳答案

根据以下 log4j 链接 https://logging.apache.org/log4php/docs/appenders/rolling-file.html ,您必须添加以下行。

log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=250000

注意:

  1. 文件追加器应为RollingFileAppender
  2. 触发策略应为SizeBasedTriggeringPolicy

编辑:

# Direct log messages to a log filelog4j.appender.file.encoding=UTF-8
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.File=${LOG_DIR}/application.log
log4j.appender.file.RollingPolicy.FileNamePattern=./logs/application_%d{dd-MM-yyyy}.log
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.MaxFileSize=250MB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.append=true

您可以引用下面的完整示例。 https://howtodoinjava.com/log4j/log4j-rolling-file-appender/

关于java - Log4J 2配置: How to split log files by size and by day at the same time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com