Setting a log file name to include current date in Log4j

191,661

Solution 1

DailyRollingFileAppender is what you exactly searching for.

<appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="application.log" />
    <param name="DatePattern" value=".yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" 
          value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n  %-5p %m%n"/>
    </layout>
  </appender>

Solution 2

Using log4j.properties file, and including apache-log4j-extras 1.1 in my POM with log4j 1.2.16

log4j.appender.LOGFILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.LOGFILE.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.LOGFILE.RollingPolicy.FileNamePattern=/logs/application_%d{yyyy-MM-dd}.log

Solution 3

I'm 99% sure that RollingFileAppender/DailyRollingFileAppender, while it gives you the date-rolling functionality you want, doesn't have any way to specify that the current log file should use the DatePattern as well.

You might just be able to simply subclass RollingFileAppender (or DailyRollingFileAppender, I forget which is which in log4net) and modify the naming logic.

Solution 4

I don't know if it is possible in Java, but in .NET the property StaticLogFileName on RollingFileAppender gives you what you want. The default is true.

<staticLogFileName value="false"/>

Full config:

<appender name="DefaultFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="application"/>
  <staticLogFileName value="false"/>
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="yyyy-MM-dd&quot;.log&quot;" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  </layout>
</appender>

&quot;.log&quot; is for not letting the dateformat recognice the global date pattern 'g' in log.

Solution 5

I have created an appender that will do that. http://stauffer.james.googlepages.com/DateFormatFileAppender.java

/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.  */

package sps.log.log4j;

import java.io.IOException;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.*;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
 * DateFormatFileAppender is a log4j Appender and extends 
 * {@link FileAppender} so each log is 
 * named based on a date format defined in the File property.
 *
 * Sample File: 'logs/'yyyy/MM-MMM/dd-EEE/HH-mm-ss-S'.log'
 * Makes a file like: logs/2004/04-Apr/13-Tue/09-45-15-937.log
 * @author James Stauffer
 */
public class DateFormatFileAppender extends FileAppender {

  /**
   * The default constructor does nothing.
   */
  public DateFormatFileAppender() {
  }

  /**
   * Instantiate a <code>DailyRollingFileAppender</code> and open the
   * file designated by <code>filename</code>. The opened filename will
   * become the ouput destination for this appender.
   */
  public DateFormatFileAppender (Layout layout, String filename) throws IOException {
    super(layout, filename, true);
  }

  private String fileBackup;//Saves the file pattern
  private boolean separate = false;

  public void setFile(String file) {
    super.setFile(file);
    this.fileBackup = getFile();
  }

  /**
   * If true each LoggingEvent causes that file to close and open.
   * This is useful when the file is a pattern that would often
   * produce a different filename.
   */
  public void setSeparate(boolean separate) {
    this.separate = separate;
  }

  protected void subAppend(LoggingEvent event) {
    if(separate) {
        try {//First reset the file so each new log gets a new file.
            setFile(getFile(), getAppend(), getBufferedIO(), getBufferSize());
        } catch(IOException e) {
            LogLog.error("Unable to reset fileName.");
        }
    }
    super.subAppend(event);
  }


  public
  synchronized
  void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
                                                            throws IOException {
    SimpleDateFormat sdf = new SimpleDateFormat(fileBackup);
    String actualFileName = sdf.format(new Date());
    makeDirs(actualFileName);
    super.setFile(actualFileName, append, bufferedIO, bufferSize);
  }

  /**
   * Ensures that all of the directories for the given path exist.
   * Anything after the last / or \ is assumed to be a filename.
   */
  private void makeDirs (String path) {
    int indexSlash = path.lastIndexOf("/");
    int indexBackSlash = path.lastIndexOf("\\");
    int index = Math.max(indexSlash, indexBackSlash);
    if(index > 0) {
        String dirs = path.substring(0, index);
//        LogLog.debug("Making " + dirs);
        File dir = new File(dirs);
        if(!dir.exists()) {
            boolean success = dir.mkdirs();
            if(!success) {
                LogLog.error("Unable to create directories for " + dirs);
            }
        }
    }
  }

}
Share:
191,661
Tim
Author by

Tim

Updated on July 05, 2022

Comments

  • Tim
    Tim almost 2 years

    I would like to set the log file name for a log4j and log4net appender to have the current date. We are doing Daily rollovers but the current log file does not have a date. The log file name format would be

    logname.2008-10-10.log
    

    Anyone know the best way for me to do this?

    edit: I forgot to mention that we would want to do this in log4net as well. Plus any solution would need to be usable in JBoss.

  • Tim
    Tim over 15 years
    Won't this simply create a log file called "application.log" and only place the datepattern in the rolled log files?
  • Leo
    Leo over 15 years
    As a result you will get a separate log file for each day. But the todays log file will be named as application.log, without a date. And it's ok in most cases.
  • gyrolf
    gyrolf over 15 years
    Two comments on "DatePattern": - I use ".yyyy-MM-dd.lo\g" to get the same extension for all log files. - The backslash before g is needed (at least for log4net) to prevent .Net applying the predefined "g" format.
  • James McMahon
    James McMahon over 14 years
    @gyrolf, applying the format you mention, do your logs end up as mylog.log.yyyy-MM-dd.log? Or are you somehow removing the previous extension?
  • Alicia
    Alicia over 13 years
    This is exactly what I was looking.
  • Alex
    Alex over 11 years
    ^---^ Pro level: 99 log4j.appender.out.RollingPolicy.FileNamePattern=logs/%d{yyy‌​y-MM-dd HH-mm-ss} cron-script-x/out.log log4j.appender.err.RollingPolicy.FileNamePattern=logs/%d{yyy‌​y-MM-dd HH-mm-ss} cron-script-x/err.log
  • mmutilva
    mmutilva about 11 years
    As of right now, the download mirrors links are all broken. You can still find the library in the archive repository: archive.apache.org/dist/logging/log4j/companions/extras
  • keisar
    keisar over 9 years
    From log4j docs: DailyRollingFileAppender has been observed to exhibit synchronization issues and data loss.(logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/‌​…). you should really follow @shinds answer
  • Cléssio Mendes
    Cléssio Mendes about 9 years
    Be carefull with the slashes "/" in the file name. I would rather user: "logname"+new Date().format("yyyy-mm-dd-hh-MM")+".log"
  • Mr. Cat
    Mr. Cat about 9 years
    This will create new log file only on app start and will not cut your logs to files by date
  • Matt_Bro
    Matt_Bro almost 9 years
    This is not what the OP asks for. This solution will not have the current date.
  • chrki
    chrki over 8 years
    Just for clarification: Are the HTML entities &quot in yyyy-MM-dd&quot;.log&quot; meant to be there, instead of '?
  • ptha
    ptha over 8 years
    @JamesMcMahon I've used log4j with a log4j.properties file like so: log4j.appender.roll.datePattern='.'yyyy-MM-dd'.txt' and yes this results in a file name like application.log.2016-01-25.log
  • vsingh
    vsingh about 8 years
    log4j.appender..DatePattern=yyyy-MM-dd will do the trick. File name will roll every day and no need for additional dependency