问题描述
log4j 1.2是否为每日存档日志提供任何机制?
每个人都说我可以通过org.apache.log4j.rolling.timebasedrollingpolicy做到这一点,但在1.2.15的来源中,我看不到任何timebaseedRollingPolicyPolicy类.
我找到了一个分辨率:
<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>
推荐答案
您需要将您的appender定义为dailyrolllingfileappender,并将日期模式定义为最新的粒度.以下是一个名为"文件"的示例的示例,该示例将输出到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
然后,您将需要定义登录器(或rootlogger)以输出此Appender.例如:
log4j.rootLogger=debug, file
其他推荐答案
您可以使用 dailyrolllingfileappender .
其他推荐答案
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
在我的应用程序中,在上面的上面配置log4j.appender.file.properties文件似乎在我的springbootapplication中不起作用.最后,我使用了LogBack-spring.xml文件解决方案.该文件放置在SRC/MAIN/RECORCES中(您通常放置Application.properties文件的默认文件夹).
您的pom.xml中不需要Maven依赖关系,但在您的应用程序的根目录中创建A/Logs/存档文件夹.文件" namefyourlog.log"将在第二天自动存档.由于此文件夹位于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>
问题描述
Does log4j 1.2 provide any mechanism to daily archive log?
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.
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>
推荐答案
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.
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:
log4j.rootLogger=debug, file
其他推荐答案
What you ask can be done using DailyRollingFileAppender.
其他推荐答案
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).
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.
<?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>