Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create log file with date using log4j

I'm writing my log file using below code but it stores file as QueryLog.log. Am i missing something? Check my code of log4j.properties file

log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd-a
log4j.appender.FILE.File=log4j/QueryLog.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern= %d{HH:mm:ss} %-5p %c - %m%n

Links i used:

http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

http://www.codejava.net/coding/configure-log4j-for-creating-daily-rolling-log-files

like image 435
Vicky Thakor Avatar asked Sep 10 '13 13:09

Vicky Thakor


3 Answers

As is mentioned in this StackOverflow Q&A, the purpose of a RollingFileAppender is to automatically create a new log file at some defined interval. In the case of the DailyRollingFileAppender, that interval is 12:00 AM of each day.

What this means is that the first file created by log4j will have the file name you specified here:

log4j.appender.FILE.File=log4j/QueryLog.log

And, from then forward, each day a new log file will be created with the date appended to it.

To always name the file with the date appended, you could use DatedFileAppender by Geoff Mottram

like image 120
crush Avatar answered Sep 29 '22 09:09

crush


This line sets the log file name, in your log4j properties you have: log4j.appender.FILE.File=log4j/QueryLog.log

You can see the answer here Setting a log file name to include current date in Log4j

like image 35
jandresrodriguez Avatar answered Sep 29 '22 09:09

jandresrodriguez


The solution to log directly to a file with current active date/time such as XYZ.log.20150101.log instead of XYZ.log could be done by simply removing ActiveFileName property when using the rolling package org.apache.log4j.rolling.RollingFileAppender in the apache-log4j-extras 1.1 with log4j 1.2.x.

<appender name="defaultFileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="append" value="true" />
    <param name="Threshold" value="INFO" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern"
            value="${catalina.base}/logs/application/custom-application-logger.%d{yyyy-MM-dd_HH_mm}" />
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %-10t [%-40.40c] %x - %m%n" />
    </layout>
</appender>
like image 39
kisna Avatar answered Sep 29 '22 09:09

kisna