滚动日志文件和删除旧的日志文件 [英] Rolling log Files & removing old log files
问题描述
我正在开发一个基于Java SOAP的Web服务应用程序,在其中我将stdout写入文本文件作为日志,以供我们参考.该文件正在急剧增长,因此我需要检查文件的大小...例如,如果文件大小超过10 Mb,则必须创建另一个文件.
I am working on a Java SOAP based webservice application where I am writing stdout to a text file as log for our reference. That file is growing enormously, so I need to check for the size of the file... For example if the file size crosses 10 Mb, I have to create another file.
像这样,我必须创建10个文件,一个接一个地旋转,直到十个文件.达到十个文件后,我必须删除起始文件,然后重新开始创建...
Like this, I have to create 10 files, rotating one after the other until ten files. After reaching ten files, I have to delete the starting files and start creating again...
在否之后如何删除文件.文件将变为10?
How can I delete files after the no. of files will become 10?
推荐答案
我使用 logback 来做到这一点.下面的示例是基于时间的滚动策略.视您在日志中输出的数据量而定,此方法可能会按原样工作.
I use logback to do this. The example below is a time based rolling policy. Depending upon how much data your outputting during your logs, this may work for you as-is.
此外,作为奖励,我的配置文件将日志扔到HTML中,从而使想要查看该日志文件的管理类型更易于查看.
Also, as a bonus, my config file tosses the log into HTML to make it easy to view for management types who want to look though the log file.
配置文件的相关部分:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs\logFile.html</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -- >
<fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 10MB -- >
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 10 days' worth of history -- >
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
</layout>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
相关的Maven相关性:
relevant Maven dependancies:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.12</version>
</dependency>
这篇关于滚动日志文件和删除旧的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!