6

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

Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
  • 1
    The first file will simply be named `log4j/QueryLog.log`. When your logs roll over from there, they will have the data appended to them. This is why it is a `RollingFileAppender`. – crush Sep 10 '13 at 13:20
  • ohh. I got it now. he is expecting file to have date appended today itself. – Ashay Thorat Sep 10 '13 at 13:22
  • @VickyThakor See this Q&A: http://stackoverflow.com/questions/8798794/log-file-name-to-include-current-date-in-log4j – crush Sep 10 '13 at 13:23
  • @Vicky Your current date logs will always be stored in file without date appended to it. When day passes. your older file will be renamed to have yesterday's date appended to filename and new file will conatain current day's logs – Ashay Thorat Sep 10 '13 at 13:25

3 Answers3

6

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

Community
  • 1
  • 1
crush
  • 16,713
  • 9
  • 59
  • 100
1

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

Community
  • 1
  • 1
jandresrodriguez
  • 794
  • 7
  • 18
1

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>
kisna
  • 2,869
  • 1
  • 25
  • 30