Java log4j 属性 DailyRollingFileAppender 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21928972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:27:49  来源:igfitidea点击:

log4j properties DailyRollingFileAppender does not work

javaloggingnetbeanslog4jrollingfileappender

提问by Nitin Kundapur Bhat

I want daily logs with the log file appended with the date in yyyy-dd-mm format. When I use DailyRollingFileAppender, a new log file is not created. The logs are written to the same log file. Also, the date Pattern provided is not considered. The log file created is LoggerFile.log. And every content(even on the next day) is written to this file.

我想要每日日志,日志文件附加 yyyy-dd-mm 格式的日期。当我使用 DailyRollingFileAppender 时,不会创建新的日志文件。日志被写入同一个日志文件。此外,不考虑提供的日期模式。创建的日志文件是 LoggerFile.log。并且每个内容(甚至在第二天)都写入此文件。

I am using the log4j-1.2.17 jar. I am developing in Netbeans 7.3.1 in Java.

我正在使用 log4j-1.2.17 jar。我正在用 Java 开发 Netbeans 7.3.1。

Is there anyone using this JAR and facing such a problem. Please help!

有没有人使用这个 JAR 并面临这样的问题。请帮忙!

Here is the content of the properties file I use:

这是我使用的属性文件的内容:

# Root logger option
log4j.rootLogger=ERROR,FILE,stdout

# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

log4j.appender.FILE.File=.//..//logs//LoggerFile.log

# Define the layout for file appender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS zzz} %5p     %c{1}:%L - %m%n

log4j.appender.FILE.MaxFileSize=10MB


# Direct log messages to stdout
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`

回答by Jay

Try to add another line log4j.appender.file.Append=true

尝试添加另一行 log4j.appender.file.Append=true

The full code looks like below

完整代码如下所示

            # Root logger option
            log4j.rootLogger=DEBUG, file, stdout

            # Daily rolling file appender
            log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
            log4j.appender.file.File=logs/mylogs.log
            log4j.appender.file.Append=true
            log4j.appender.file.DatePattern='.'dd-MM-yyyy
            log4j.appender.file.MaxFileSize=10MB
            log4j.appender.file.MaxBackupIndex=100
            log4j.appender.file.encoding=UTF-8
            log4j.appender.file.layout=org.apache.log4j.PatternLayout
            log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

回答by Suci

First of all you have an error because the DailyRollingFileAppenderdoes not support the property MaxFileSize.

首先,您有一个错误,因为DailyRollingFileAppender不支持该属性MaxFileSize

Then you can try to remove the char 'from the DatePattern:

然后,你可以尝试删除字符'DatePattern

Try changing

尝试改变

log4j.appender.FILE.DatePattern='.'yyyy-MM-dd

to

log4j.appender.FILE.DatePattern=.yyyy-MM-dd

回答by Santosh Kumar Arjunan

I had similar requirements of daily log file rotation.. (Though the question is older, thought the answer would help others)..

我对每日日志文件轮换有类似的要求..(虽然问题较旧,但认为答案会对其他人有所帮助)。

Key points:

关键点:

  1. First of all, we can avoid using DailyRollingFileAppender. Why? DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss. The log4j extras companion includes alternatives which should be considered for new deployments and which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender. Reference: Documentation of Daily Rolling File Appender

  2. Apache extras log4j, Would suggest to use org.apache.log4j.rolling.RollingFileAppenderwith the Time based rolling policy

  3. May find a sampleconfiguration for time based rotation of log.

  1. 首先,我们可以避免使用 DailyRollingFileAppender。为什么?已观察到 DailyRollingFileAppender 出现同步问题和数据丢失。log4j extras 伴侣包括新部署应考虑的替代方案,并在 org.apache.log4j.rolling.RollingFileAppender 的文档中讨论。参考:Daily Rolling File Appender 的文档

  2. Apache extras log4j,建议将org.apache.log4j.rolling.RollingFileAppender基于时间的滚动策略一起使用

  3. 可以找到基于时间的日志轮换的示例配置。

Hope this helps.

希望这可以帮助。