Logback有时不写入日志文件,有时不滚动日志文件

发布于 2024-09-16 13:20:43 字数 1782 浏览 4 评论 0原文

有时,当我启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-09-23 13:20:43

要调试该问题,请使用 并且不要重定向 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.

以往的大感动 2024-09-23 13:20:43

事实证明,这与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文