Logback有时不写入日志文件,有时不滚动日志文件
有时,当我启动 java 应用程序时,logback 拒绝向我的日志文件写入任何内容。有时,它还会拒绝在午夜(或午夜后的第一个日志记录事件)滚动日志文件,这会导致日志记录事件丢失。当我查看我的主日志文件时,logbacks 无法滚动日志,它将有一个像 23:59 这样的时间,带有昨天的日期,并且该时间之后的任何和所有日志记录都将无法挽回地丢失。我有一个相当简单的配置文件,它看起来是正确的。它当然应该是正确的,因为它在大多数情况下都有效。
这是我的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<!--and http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy-->
<!--for further documentation-->
<append>true</append>
<File>aggregator.log</File>
<encoder>
<!-- was: %d{yyyy-MM-dd HH:mm:ss}%5p [%t] (%F:%L) - %msg%n -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- By setting the name to .gz here, we get free compression. -->
<fileNamePattern>aggregator.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</root>
</configuration>
不幸的是,我无法可靠地重现此错误,因此调试它有点困难。有人可以告诉我我做错了什么,或者还有什么问题吗?如果有任何帮助,我将 STDOUT 和 STDERR 重定向到 /dev/null (顺便说一句,我在 Linux 上运行)。
Sometimes when I launch my java application, logback refuses to write anything to my logfile. Sometimes it also refuses to roll the logfile at midnight (or at the first logging event after midnight), which results in logging events being lost to the void. When i look at my main log file when logbacks has failed to roll the log, it will have a time like 23:59, with yesterday's date, and any and all logging statements after that time will be irretrievably lost. I have a fairly simple configuration file, and it looks correct. It certainly should be correct, as it works most of the time.
Here's my configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<!--and http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy-->
<!--for further documentation-->
<append>true</append>
<File>aggregator.log</File>
<encoder>
<!-- was: %d{yyyy-MM-dd HH:mm:ss}%5p [%t] (%F:%L) - %msg%n -->
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- By setting the name to .gz here, we get free compression. -->
<fileNamePattern>aggregator.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] \(%class{25}:%line\) - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</root>
</configuration>
Unfortunately, I cannot reliably reproduce this error, so debugging it is a bit difficult. Could someone tell me either what I'm doing wrong, or what else might be the problem? If it's of any help, I redirect STDOUT and STDERR to /dev/null (I run on linux, btw).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要调试该问题,请使用
并且不要重定向 stdout。 Logback 会在解析配置时以及出现问题时打印消息。To debug the issue, use
<configuration debug="true">
and don't redirect stdout. Logback will print messages there as it parses the config and when something goes wrong.事实证明,这与 logback 根本没有什么关系。问题是我有一个 .policy 文件没有为应用程序指定适当的权限。当我认为我能够旋转文件时,我已经手动移动或删除了以前的文件。我通过确保 logback 有足够的权限来轮换自己的日志来解决这个问题。
It turns out that this had very little to do with logback at all. The problem was that I had a .policy file that didn't specify the proper permissions for the applications. The times when I thought I managed to rotate files was times when I had moved or deleted the previous ones by hand. I solved this by making sure that logback had sufficient permissions to rotate its own logs.