问题描述
是否有任何流行的Java日志记录框架支持滚动文件Appender,我可以每天配置为滚动,还可以删除任何一段时间内的日志文件吗?我知道我可以使用滚动文件的appender和cron,但是想知道是否有人知道可以同时做的appender.
推荐答案
logback的经典滚动fileappender提供了更多内容.手册中的示例配置( http://logback.qos.qos.cos.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>
这提供了每日翻车和30天的历史.将其放在一个名为logback.xml的文件中,或用于测试树的logback-test.xml,然后将其放在classPath中.
其他推荐答案
的确,如果您使用的是log4j,则可以使用此appender:
它非常非常非常易于使用,只需下载整个班级(是,上述链接中的类代码),将其包括在项目中(无论您在哪里想要,您可以更改包中的软件包),然后配置这样的log4j.properties文件( 此文件必须在您的classpath中,例如在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>
就是这样!
其他推荐答案
在Log4J上工作后,我读到了木回信,并设计为 log4j ,我不得不说,logback是宽的步进!
例如,要配置此滚动文件appender并缩回旧日志,最多为30,在log4j上您必须执行一些代码更改(请查看我的预览 andanswer这样的配置文件:
<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,一个邮政编码.
宽比log4j!
更容易如果要使用更多配置选项,只需检查此链接.
问题描述
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.
推荐答案
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.
其他推荐答案
Indeed, if you are using Log4J you can use this appender:
It's very, very, VERY easy to use, just download the whole class(yes, the code of the class in the above link), include it in your project (wherever you want, you can change the package to match inside your project), and then configure the log4j.properties file like this(this file has to be in your classpath, for example in the src/main/resources folder):
# 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
and besides this just add the dependency to log4j into your pom like this:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
And that's it!
其他推荐答案
After working a while on Log4j, I read that Logback was developed and designed to be the successor of Log4j, and I have to say, Logback is wide stepper!
For example, to configure, this rolling file Appender, and zip the older logs, and have a maximum of 30, on Log4J you have to perform some code changes (please review my previews answer for more details), but on logback, everything is done on the config file like this:
<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>
And that's it, you will have the current log unzipped, and the older days, zip, one zip file by each day.
Wide more easier than log4j!
If you want to play with more config options, just check this link.