Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does log4j provide any mechanism to daily archive log?

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>
like image 260
breedish Avatar asked Jan 05 '11 22:01

breedish


People also ask

How does log4j logging work?

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).

Which log4j component is responsible for logging messages?

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.

Where are log4j logs stored?

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.

How do you create a new log file for each time the application runs log4j?

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.


2 Answers

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
like image 145
jbx Avatar answered Oct 15 '22 22:10

jbx


What you ask can be done using DailyRollingFileAppender.

like image 40
darioo Avatar answered Oct 15 '22 20:10

darioo