Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Configure Log4j to Generate New Log File for Every Run

5.00/5 (1 vote)
20 Apr 2016CPOL1 min read 50.5K  
Log4j trick to generate new log file every time you run the application

Introduction

Log4j is a good logging framework. It writes logs in the file which we specified in log4j.properties. We can use org.apache.log4j.<wbr />RollingFileAppender to write to another file once file exceeds the limit we had specified in the configuration file. We can also use org.apache.log4j.<wbr />DailyRollingFileAppender which is writing to different file depending on DatePattern we had specified in the configuration file.

But in Log4j, there is no FileAppender to create new log file every time you run your code. But we can use some hacks to create a new log file every time we run the code.

Using the Code

Let us see how to do that. First, we need to create new System properties and need to load it with a current time stamp like this:

Java
static{
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
        System.setProperty("current.date.time", dateFormat.format(new Date()));
    }

We will add this code in static block because JVM executes static blocks statement while loading classes into memory.

Now Add key log4j.appender.file.File with system properties name and some prefix text if you want.

Java
log4j.appender.file.File=Log4jDemo_${current.date.time}.log

This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss.log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

Complete code: Log4jDemo.java

Java
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class Log4jDemo {

    static{
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-hh-mm-ss");
        System.setProperty("current.date.time", dateFormat.format(new Date()));
    }
    
    final static Logger log = Logger.getLogger(Log4jDemo.class);
    
    public static void main(String[] args) {
        
        log.trace("This is Trace Message.");
        log.debug("This is Debug Message.");
        log.info("This is Info Message.");
        log.warn("This is Warn Message.");
        log.error("This is Error Message.");
        log.fatal("This is Fatal Message.");        
    }
}

log4j.properties file:

log4j.rootLogger=ALL, stdout, file

# Redirect logs to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect logs to a file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=Log4jDemo_${current.date.time}.log
log4j.appender.file.Append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Points of Interest

Sometimes, we need to maintain old logs. In such cases, we can use this trick to create a new log file every time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)