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>
Log4j allows logged messages to contain format strings that reference external information through the Java Naming and Directory Interface (JNDI). This allows information to be remotely retrieved across a variety of protocols, including the Lightweight Directory Access Protocol (LDAP).
log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.
The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.
File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With