gpt4 book ai didi

java - Log4j MaxFileSize 与 DailyRollingFileAppender

转载 作者:行者123 更新时间:2023-11-30 06:15:14 27 4
gpt4 key购买 nike

我的目标是每天生成日志文件,但MaxFileSize=100KB

所以我使用了 DailyRollingFileAppender,因为我使用的是 log4j 2.2 版本

日期工作正常,当我使用 MaxFileSize 时,它​​不起作用,并且未根据文件大小创建新文件,但日期仍在工作。

问题:MaxFileSize 无法与 DailyRollingfileAppender 的 DatePattern 配合使用

这是 log4j.properties 文件代码:

log4j.rootLogger=DEBUG, stdout, file

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

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.file.File=${catalina.home}/logs/abc
log4j.appender.file.MaxFileSize=80KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log'

日期模式需要更改一些内容,但还不知道

最佳答案

尝试以下配置来根据时间和大小轮换文件 -

status = warn
name= properties_configuration

property.basePath = ${catalina.home}/logs/

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %level %c{1}:%L - %msg%n
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${basePath}/abc.log
appender.rolling.filePattern=${basePath}abc_%d{yyyyMMdd}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %level (%F:%L) - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 100 KB

# write your package name as the value of this key
logger.example.name = log4j2.example
logger.example.level = info
logger.example.additivity = false
logger.example.appenderRef.rolling.ref = fileLogger
logger.example.appenderRef.console.ref = consoleLogger

rootLogger.level = error
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger

关于java - Log4j MaxFileSize 与 DailyRollingFileAppender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365388/

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