java log4j 是否提供任何机制来每日归档日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4609909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does log4j provide any mechanism to daily archive log?
提问by breedish
Does log4j 1.2 provide any mechanism to daily archive log?
log4j 1.2 是否提供任何机制来每日归档日志?
Everybody say that i can do it via org.apache.log4j.rolling.TimeBasedRollingPolicy but in sources of 1.2.15 i don't see any TimeBasedRollingPolicy class.
每个人都说我可以通过 org.apache.log4j.rolling.TimeBasedRollingPolicy 来完成,但在 1.2.15 的源代码中我没有看到任何 TimeBasedRollingPolicy 类。
I found a resolution :
我找到了一个解决方案:
<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="${jboss.server.log.dir}/server.log"/>
<!-- roll log file once a day -->
<param name="FileNamePattern" value="${jboss.server.log.dir}/archives/server.log.%d.gz"/>
</rollingPolicy>
<!-- A PatternLayout that limits the number of lines in stack traces -->
<layout class="com.mtvi.log4j.StackTraceLimitingPatternLayout">
<!-- The full pattern: Date MS Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
采纳答案by jbx
You need to define your appender as DailyRollingFileAppender and define the date pattern to be up to day granularity. The following is an example appender named 'file' that outputs to application.log and rolls the file daily by appending the date to the end after midnight and starting a new file.
您需要将您的 appender 定义为 DailyRollingFileAppender 并将日期模式定义为达到天粒度。以下是一个名为“file”的示例 appender,它输出到 application.log 并通过在午夜之后将日期附加到末尾并开始一个新文件来每天滚动文件。
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=application.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] - %m%n
You will then need to define your loggers (or rootLogger) to output to this appender. For example:
然后你需要定义你的记录器(或 rootLogger)来输出到这个 appender。例如:
log4j.rootLogger=debug, file
回答by darioo
What you ask can be done using DailyRollingFileAppender.
您可以使用DailyRollingFileAppender完成您的要求。
回答by lazyduckiy
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=application.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] - %m%n
Configuring log4j.Appender.file as per above in my application.properties file doesn't seem to work in my SpringBootApplication. In the end, I used the logback-spring.xml file solution. The file is placed in src/main/recources (the default folder where you usually put your application.properties file).
如上所述在我的 application.properties 文件中配置 log4j.Appender.file 在我的 SpringBootApplication 中似乎不起作用。最后,我使用了 logback-spring.xml 文件解决方案。该文件位于 src/main/recources(您通常放置 application.properties 文件的默认文件夹)。
No maven dependency required in your pom.xml but create a /logs/archived folder in the root directory of your application. The file 'nameOfYourLog.log' will be automatically archived on the next day into the ./logs/archived folder. Since this folder is positioned outside of your jar file, it is easily accessible to view the log (for the desired date) when it is deployed into a production server.
在 pom.xml 中不需要 maven 依赖,但在应用程序的根目录中创建一个 /logs/archived 文件夹。文件“nameOfYourLog.log”将在第二天自动归档到 ./logs/archived 文件夹中。由于此文件夹位于 jar 文件之外,因此在将其部署到生产服务器时可以轻松访问查看日志(针对所需日期)。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="./logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<!--<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/nameOfYourLog.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.the.package.you.wish.to.log" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
回答by Agi Hammerthief
Use log4j-extras
:
使用log4j-extras
:
<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="${jboss.server.log.dir}/server.log"/>
<!-- roll log file once a day -->
<param name="FileNamePattern" value="${jboss.server.log.dir}/archives/server.log.%d.gz"/>
</rollingPolicy>
<!-- A PatternLayout that limits the number of lines in stack traces -->
<layout class="com.mtvi.log4j.StackTraceLimitingPatternLayout">
<!-- The full pattern: Date MS Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
Note: This answer was extracted from the OP's question, in order to preserve the correct format mandated by Stack Exchange. (The OP clearly didn't respond to jbx's comment.)
注意:此答案摘自 OP 的问题,以保留 Stack Exchange 规定的正确格式。(OP 显然没有回应 jbx 的评论。)