Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control log file with daily rolling and max file size by log4j?

I would like to create log file that can be rolled at the beginning of the next day or if it's reached to specified file size and log file must be contained inside date folder. Format of folder is YYYYMMDD (/20111103/mylogfile.log)

Is it possible to do this by Log4j without implementing custom class?

Now I am using log4j and log4j-extra, I set FileNamePattern attribute as defined in log4j API to rolling my file everyday and set max file size to 50 MB.

My log4j.xml is:

<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="maxFileSize" value="50000000" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
    </layout>
</appender>

Result of above setting is that log file is not rolled at the beginning of next days but if file's size reached to 50 MB, log file will be rolled.

Please help to advise me. m(_ _)m

like image 200
Naphatthara P Avatar asked Nov 03 '11 11:11

Naphatthara P


People also ask

What is rolling file in log4j?

Java Logging. Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

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.

What is the purpose of MaxBackupIndex configuration of rolling file Appender?

The MaxBackupIndex option determines how many backup files are kept before the oldest is erased. This option takes a positive integer value. If set to zero, then there will be no backup files and the log file will be truncated when it reaches MaxFileSize .


2 Answers

Daily works for me with xml in question only transformed into log4j.properties equivalent to roll over after 100KB (for testing purposes):

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=/path/to/logs/%d{yyyyMMdd}/myLog_%d{yyyyMMddHH}.log
log4j.appender.file.TriggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.TriggeringPolicy.maxFileSize=100000
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true

This will create a timestamped directory (one directory per day - optional) with logs with the following format (one per hour for testing purposes - change FileNamePattern to suit your needs):

myCompanyLog_201602031030.log

myCompanyLog_201602031130.log

like image 199
arush436 Avatar answered Oct 20 '22 23:10

arush436


To enable the daily rolling: class="org.apache.log4j.DailyRollingFileAppender"

And to enable the max file size and the number of backup files

<param name="MaxFileSize" value="200MB" />  
<param name="MaxBackupIndex" value="4" />

But you can not put MaxFileSize with DailyRolling, so you can use rolling file appender

An example:

<appender name="MAIN_FA" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="logs/main.log" />
    <param name="datePattern" value="'-'yyyy-MM-dd'.log'" />
    <param name="append" value="false" />
    <param name="Threshold" value="ALL" />
    <param name="MaxFileSize" value="200MB" />  
    <param name="MaxBackupIndex" value="4" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
    </layout>
</appender>

Or you can do this http://wiki.apache.org/logging-log4j/DailyRollingFileAppender

like image 43
Alberto Perez Avatar answered Oct 21 '22 01:10

Alberto Perez