1

I have created an appender that create and write logs to file with current date in filename, like this:

filename_18052014.log

The problem is that my appender should create new file at 00:00:00 every day and write logs there. But it doesn't happen, it still writes to old file and new file is not created...

My code:

package com.comarch.log4j;

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

import org.apache.log4j.FileAppender;

public class DateFormatFileAppender extends FileAppender {
    private String fileName = "";

    public DateFormatFileAppender() {
        setFile("");
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException {
        super.setFile(generateFileName(), append, bufferedIO, bufferSize);
    }

    private String generateFileName() {
        SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
        String dateString = df.format(new Date());
        return String.format("%s_%s.log", getFileName(), dateString);
    }

}

Have you any idea, how to do it?

What do you think about this solution? Method append is extended.

package com.comarch.log4j;

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

import org.apache.log4j.FileAppender;

public class DateFormatFileAppender extends FileAppender {
    private String fileName = "";
    private int day;

    public DateFormatFileAppender() {
        setFile("");
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) throws IOException {
    if (!getFile().equals(""))
        super.setFile(fileName, append, bufferedIO, bufferSize);
    }

public void append(LoggingEvent event) {
    if (Calendar.DAY_OF_MONTH != day) {
        setFile(generateFileName());
        activateOptions();
    } 
    super.append(event);
}

    private String generateFileName() {
        SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
        String dateString = df.format(new Date());
        return String.format("%s_%s.log", getFileName(), dateString);
    }

}
David Silva
  • 1,939
  • 7
  • 28
  • 60
  • Why not use a [`RollingFileAppender`](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/RollingFileAppender.html) instead? Or better yet, have the appender taken care of [in configuration](http://stackoverflow.com/questions/5117758/configuring-rollingfileappender-in-log4j)? – Makoto May 18 '14 at 18:49
  • @Makoto - could you write me more details? I dont know if RollingFileAppender is good choice for me. Appender should direct logs to filename_18052014.log for today, and tommorow to filename_18052014.log etc. Where filename should be configured. – David Silva May 18 '14 at 19:20
  • A rolling file appender would take care of that for you, though; all you need to provide is the filename, and it will adjust the date. Admittedly though, your for-instance has both days staying the same, which is a bit concerning. – Makoto May 18 '14 at 19:25
  • @Makoto, I edited my post - what do you think about my solution? I extended append method and check if file should be changed. What about performance? – David Silva May 18 '14 at 21:28
  • I maintain - this solution is already captured by existing implementations of `RollingFileAppender`, and existing Log4J configuration settings. – Makoto May 18 '14 at 21:42

0 Answers0