0

I change file appender file name in log4j at runtime.

now I want the contents of previous file to be in the new file for the appender.

Can somebody tell me how to do this ?

Initially I have log4j.properties as:

#log4j.appender.H.File='/home/g/connector/logs/'Y'_H.log'
#log4j.appender.H.File=/home/g/connector/logs/H.log
log4j.appender.H.File=/home/g/${logNameSuffix}.log

In my program, I initially do:

    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd.HH:mm:ss");
    formattedDate = "jam" + formatter.format(date) + "_" + port;
    System.setProperty("logNameSuffix", formattedDate);
    PropertyConfigurator.configure(otherArgs[1]);

Then re-load as:

    String logName = formattedDate;

    System.clearProperty("logNameSuffix");
    System.setProperty("logNameSuffix", logName);

    LogManager.resetConfiguration();

what i want is that the new log created should get the old log contents also in it.

Is it possible with log4j ?

Thanks, JJ

user656189
  • 139
  • 8
  • 17
  • Why? That kind of defeats the purpose of creating a timestamped filename, and it's just as easy (easier) to deal with file concatenation on the command line. – Dave Newton Feb 06 '12 at 17:20
  • This is becasue at time of program invocation, i don't have log file name. But I get filename sometime later in exectuon, when i have create log with that name and get all previous contents also in this new log file. – user656189 Feb 06 '12 at 17:24

1 Answers1

0

I think you should do it manualy: rename old file and then reset properties:

copyFile(new File("old file"), new File("new file"));

See copyFile

Also you can avoid System properties, and don't re-read property file, use this:

((FileAppender) LogManager.getRootLogger().getAppender("File")).setFile("new file");
Community
  • 1
  • 1
kornero
  • 1,109
  • 8
  • 11