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.
You will then need to define your loggers (or rootLogger) to output to this appender. For example:
What you ask can be done using DailyRollingFileAppender.
Use
log4j-extras
: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.)
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.