Thursday, January 3, 2008

Change log4j properties dynamically

Most of the time,we will define log4j properties in a file.But sometimes,we might need to modify the properties value dynamically,instead of keep modifying the properties file.Example,we want to log a file with the timestamp as file name everytime we run the application.So, here is how we do it.

In the properties file,we will have something like this.
log4j.appender.fileAppender.layout.ConversionPattern=%d [%-5p] %c{1} - %m%n
You will notice that we have a ${logFileName} in our properties file.This is meant to be substituted later.So when we run the application,we will type the following command
java -DlogFileName=20080104.log YourClass
Happy programming. :)

No comments: