5

Do any of the popular Java logging frameworks support a rolling file appender, that I can configure to rollover daily, and also delete any log file that is over some number of days old? I know I could use a rolling file appender and a cron, but was wondering if anyone knew of an appender that can do both.

4

4 回答 4

3

Logback's classic RollingFileAppender provides this and more. An example configuration from the manual (http://logback.qos.ch/manual/appenders.html#onRollingPolicies)

 <configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

This provides daily rollover and 30 days of history. Place this in a file called logback.xml, or logback-test.xml for test trees, and place it in the classpath.

于 2013-07-29T23:02:19.657 回答
1

事实上,如果你使用 Log4J,你可以使用这个 appender:

它非常非常非常易于使用,只需下载整个类(是的,上面链接中的类的代码),将其包含在您的项目中(您可以在任何地方更改包以匹配您的项目) ,然后像这样配置 log4j.properties 文件(此文件必须在您的类路径中,例如在 src/main/resources 文件夹中):

# Define the root logger with appender file
log = log
log4j.rootLogger = TRACE, FILE

# Define the logical path where you put the class you downloaded from "blog.kimb3r.com" link (above)
log4j.appender.FILE=com.yourapp.yourpackage.log.CustodianDailyRollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
log4j.appender.FILE.File=${log}/yourlogfile.log
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
# How many files you want to keep?, in this case I'm having just 15 days of files, one file per day:
log4j.appender.FILE.MaxNumberOfDays=15
# If True, the older files will be compressed into a zip file (*which will save you a lot of space on the server*)
log4j.appender.FILE.CompressBackups=true

除此之外,只需将 log4j 的依赖项添加到您的 pom 中,如下所示:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

就是这样!

于 2015-04-30T15:06:55.780 回答
1

在 Log4j 上工作了一段时间后,我读到 Logback 被开发和设计为Log4j的继任者,我不得不说,Logback 是广泛的步进器!

例如,要配置此滚动文件 Appender,并压缩较旧的日志,最多 30 个,在 Log4J 上您必须执行一些代码更改(请查看我的预览答案以获取更多详细信息),但在 logback 上,一切都是像这样在配置文件上完成:

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>log/logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>log/logFile.%d{yyyy-MM-dd}.log.zip</fileNamePattern>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>[%-5level]: [%logger{35}] - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="trace">
        <appender-ref ref="FILE" />
    </root>
</configuration>

就是这样,您将解压缩当前的日志,以及以前的 zip 文件,每天一个 zip 文件。

比 log4j 更容易!

如果您想使用更多配置选项,只需检查此链接

于 2015-05-04T20:17:02.650 回答
0

为什么不看看可能会有所帮助?

您可能可以设置最大天数来保留日志。

log4j.rootLogger=INFO,文件
log4j.appender.FILE=ca.justtechnologies.utils.logging.CustodianDailyRollingFileAppender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{ MMM dd yyyy HH:mm:ss,SSS} [%t] %-5p %l - %m%n log4j.appender.FILE.File=/var/log/web-apps/Dashboard.log log4j.appender.FILE .DatePattern='.'yyyy-MM-dd

log4j.appender.FILE.MaxNumberOfDays=14

log4j.appender.FILE.CompressBackups=true

于 2013-07-29T18:58:11.367 回答