应用程序重启后没有log4j自动压缩

发布于 2024-10-08 01:55:48 字数 1111 浏览 5 评论 0原文

我正在使用 Log4J Extras< 中提供的 TimeBasedRollingPolicy 促进的自动日志滚动和压缩/a> (参见下面的配置)。

执行此日志记录的应用程序不断停止/启动是正常的,并且我注意到,如果应用程序在翻转触发事件期间停止(在本例中为每小时翻转),则不会发生自动压缩。我觉得这很奇怪,因为滚动本身(没有压缩)仍然发生并且似乎工作正常。

对于不连续运行的应用程序,是否无法进行日志压缩?

有谁知道如何让它与 Log4J 一起工作?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true">

  <appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="FileNamePattern" value="/var/batchproc/logs/log4j_roll_compress_%d{yyyy-MM-dd-kk}.log.gz"/>
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="[%d] [%t] %-5p %c   %m%n"/>
    </layout>
  </appender>

  <root>
    <appender-ref ref="ROLL"/>
  </root>

</log4j:configuration>

I'm using the automatic log rolling and compression facilitated by the TimeBasedRollingPolicy provided in Log4J Extras (see config below).

It is normal for the application which is doing this logging to be constantly stopping/starting and I've noticed that the automatic compression does not occur if the application is stopped during a rollover triggering event (hourly rollover in this case). I find this strange as the rolling itself (without compression) still occurs and seems to work fine.

Is it not possible to have log compression work for an application that does not run continuously?

Does anyone know how to get this working with Log4J?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true">

  <appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
      <param name="FileNamePattern" value="/var/batchproc/logs/log4j_roll_compress_%d{yyyy-MM-dd-kk}.log.gz"/>
    </rollingPolicy>

    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="[%d] [%t] %-5p %c   %m%n"/>
    </layout>
  </appender>

  <root>
    <appender-ref ref="ROLL"/>
  </root>

</log4j:configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

走走停停 2024-10-15 01:55:48

仅当应用运行时记录两条不同时间单位(示例中为小时)的消息时,才会触发翻转过程。。应用程序启动时不会扫描过去的时间单位。

您可以做的一件事是使用单独的“活动”文件名作为所有日志消息在滚动/压缩之前的位置。如果您这样做,任何现有的活动日志文件都将被追加,直到又过去一个小时,然后滚动到一个 gzip 压缩的带时间戳的文件中。不幸的是,启动时不会检查该文件的时间戳(至少在 apache-log4j-extras 1.1 中),因此旧小时的日志和新小时的日志将一起保存在滚动文件中。但至少它会被压缩!

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="File" value="/var/batchproc/logs/log4j_roll_active.log"/>

    ...rest of example config here...
</appender>

The rollover process is only triggered by logging two messages that are in different time units (hours in the example) while the app is running. Past time units aren't scanned-for on app startup.

One thing you can do is use a separate "active" file name to be where all log messages go before they're rolled/zipped. If you do that, any existing active log file will be appended-to until another hour has gone by and then rolled into a gzipped, timestamped file. Unfortunately, this file's timestamp isn't checked on startup (at least in apache-log4j-extras 1.1), so the old hour's logs and new hour's logs will be together in the rolled file. But at least it'll be zipped!

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="File" value="/var/batchproc/logs/log4j_roll_active.log"/>

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