1

I'm quite new to log4j and have managed to create the logs for my code. But what I need is, a new file to be created for each run, rather than appending the logs to the same file.

Below are the properties I'm setting (found somewhere on google). Please suggest changes, so that new file will be created with the timestamp after each run.

// Here we have defined root logger
log4j.rootLogger=INFO,R,HTML

// Here we define the appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

// Here we define log file location
log4j.appender.R.File=./Logs/LastRunLog.log
log4j.appender.HTML.File=./Logs/LastRunLog.html

// Here we define the layout and pattern
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true
Shreyas SG
  • 368
  • 3
  • 6
  • 21
  • 1
    This can help https://stackoverflow.com/a/31362055/6743203 or https://stackoverflow.com/a/19133787/6743203 – Jay Smith Jun 26 '17 at 06:30

3 Answers3

3

You can use a system property to set the log4j file names with it's value and give that property an unique value for each run.

Something like this on your starter class (timeInMillis and a random to avoid name clashes):

static {
    long millis = System.currentTimeMillis();
    System.setProperty("log4jFileName", millis+"-"+Math.round(Math.random()*1000));
}

And then you refer to the system property on log4j conf properties:

log4j.appender.R.File=./Logs/${log4jFileName}.log
log4j.appender.HTML.File=./Logs/${log4jFileName}.log

Hope it helps!

Joao
  • 86
  • 5
1

You need to write (or find) a custom appender which will create the file with a timestamp in the name. The 3 defaults implementation for file logging in log4j are :

  • FileAppender : One file logging, without size limit.
  • RollingFileAppender : Multiple files and rolling file when current file hits the size limit
  • DailyRollingFileAppender : A file per day

The simpliest way is to extend FileAppender and overwrite the setFile and getFile methods.

neomega
  • 712
  • 5
  • 19
0

i think this answer would be helpful for you click here

i have ran code as the page said, and i got a new log file each time i start my application.

result like that : enter image description here

and all code in my Test.java is :`

private static final Logger log = Logger.getLogger(Test.class);

public static void main(String[] args) {
    log.info("Hello World");
}

`

Jian
  • 54
  • 3
  • Can you please provide the properties text here, the one shown in the screenshot? – Shreyas SG Jul 07 '17 at 06:23
  • Limited by the num of characters, i just show the fileOnReboot properties here, if you want the others ,i will provide it later(I don't think others are important for your question) `log4j.rootLogger=fileOnReboot log4j.appender.fileOnReboot=jian.log4j.NewFileOnRebootAppender log4j.appender.fileOnReboot.File=appLogOnReboot.log log4j.appender.fileOnReboot.layout=org.apache.log4j.PatternLayout log4j.appender.fileOnReboot.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n` – Jian Jul 07 '17 at 07:03