如何通过log4j控制日志文件的每日滚动和最大文件大小?

发布于 2024-12-13 20:34:09 字数 1257 浏览 0 评论 0原文

我想创建可以在第二天开始时滚动的日志文件,或者如果达到指定的文件大小,并且日志文件必须包含在日期文件夹内。 文件夹的格式为 YYYYMMDD (/20111103/mylogfile.log)

是否可以通过 Log4j 执行此操作而不实现自定义类?

现在我使用 log4j 和 log4j-extra, 我将 Log4j API 中定义的 FileNamePattern 属性设置为每天滚动我的文件 并将最大文件大小设置为 50 MB。

我的 log4j.xml 是:

<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="maxFileSize" value="50000000" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
    </layout>
</appender>

上述设置的结果是日志文件不会在接下来的几天开始时滚动,但如果文件的大小达到 50 MB,日志文件将被滚动。

请帮忙给我建议。毫米

I would like to create log file that can be rolled at the beginning of the next day or if it's reached to specified file size and log file must be contained inside date folder.
Format of folder is YYYYMMDD (/20111103/mylogfile.log)

Is it possible to do this by Log4j without implementing custom class?

Now I am using log4j and log4j-extra,
I set FileNamePattern attribute as defined in log4j API to rolling my file everyday
and set max file size to 50 MB.

My log4j.xml is:

<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="maxFileSize" value="50000000" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
    </layout>
</appender>

Result of above setting is that log file is not rolled at the beginning of next days but if file's size reached to 50 MB, log file will be rolled.

Please help to advise me. m(_ _)m

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

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

发布评论

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

评论(3

£噩梦荏苒 2024-12-20 20:34:09

Daily 对我来说适用,有问题的 xml 仅转换为 log4j.properties 相当于在 100KB 后滚动(用于测试目的):

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=/path/to/logs/%d{yyyyMMdd}/myLog_%d{yyyyMMddHH}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=100000
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true

这将创建一个带有时间戳的目录(每天一个目录 - 可选),其日志具有以下格式(每小时一个)出于测试目的 - 更改 FileNamePattern 以满足您的需求):

myCompanyLog_201602031030.log

myCompanyLog_201602031130.log

Daily works for me with xml in question only transformed into log4j.properties equivalent to roll over after 100KB (for testing purposes):

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=/path/to/logs/%d{yyyyMMdd}/myLog_%d{yyyyMMddHH}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=100000
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true

This will create a timestamped directory (one directory per day - optional) with logs with the following format (one per hour for testing purposes - change FileNamePattern to suit your needs):

myCompanyLog_201602031030.log

myCompanyLog_201602031130.log

半边脸i 2024-12-20 20:34:09

要启用每日滚动: class="org.apache.log4j.DailyRollingFileAppender"

并启用最大文件大小和备份文件数量

<param name="MaxFileSize" value="200MB" />  
<param name="MaxBackupIndex" value="4" />

,但不能设置 MaxFileSizeDailyRolling 一起使用,因此您可以使用滚动文件附加程序

示例:

<appender name="MAIN_FA" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/main.log" />
    <param name="datePattern" value="'-'yyyy-MM-dd'.log'" />
    <param name="append" value="false" />
    <param name="Threshold" value="ALL" />
    <param name="MaxFileSize" value="200MB" />  
    <param name="MaxBackupIndex" value="4" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
    </layout>
</appender>

或者您可以执行以下操作 http://wiki.apache.org/logging-log4j/DailyRollingFileAppender

To enable the daily rolling: class="org.apache.log4j.DailyRollingFileAppender"

And to enable the max file size and the number of backup files

<param name="MaxFileSize" value="200MB" />  
<param name="MaxBackupIndex" value="4" />

But you can not put MaxFileSize with DailyRolling, so you can use rolling file appender

An example:

<appender name="MAIN_FA" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/main.log" />
    <param name="datePattern" value="'-'yyyy-MM-dd'.log'" />
    <param name="append" value="false" />
    <param name="Threshold" value="ALL" />
    <param name="MaxFileSize" value="200MB" />  
    <param name="MaxBackupIndex" value="4" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
    </layout>
</appender>

Or you can do this http://wiki.apache.org/logging-log4j/DailyRollingFileAppender

屋檐 2024-12-20 20:34:09

来自 RollingFileAppender 文档

为了发挥任何作用,RollingFileAppender 实例必须同时设置 RollingPolicy 和 TriggeringPolicy...TimeBasedRollingPolicy 既充当 RollingPolicy 又充当
触发策略。

因此,由于上面配置了 TimeBasedRollingPolicy,因此您的 SizeBasedTriggeringPolicy 会被忽略。满足您的要求的唯一方法是自定义类实现。

另外,如果日志文件的大小确实很重要,您可以考虑使用 自动 gzip 压缩,以消除 SizeBasedTriggeringPolicy,并且只让您的日志每天滚动。

From the RollingFileAppender documentation

To be of any use, a RollingFileAppender instance must have both a RollingPolicy and a TriggeringPolicy set up.... TimeBasedRollingPolicy acts both as a RollingPolicy and a
TriggeringPolicy.

Hence, your SizeBasedTriggeringPolicy gets ignored since TimeBasedRollingPolicy is configured above. The only way to accommodate your requirement will be a custom classes implementation.

Also, If size of a log file really matters, you may consider using automatic gzip compression to eliminate SizeBasedTriggeringPolicy and only have your logs roll every day.

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