Neeraj's Blog

There is always an open source solution..

Dynamic Log File

I am now working on a project, in which I have to  create a folder structure for each output and store the outputs in that folders. For storing log file, I decided to create a new folder named “log”, inside that folder structure, and save the log file in that folder for each input.

Previously, I was craeting a single log file named “admin.log”. So for several inputs, a single log file was a mess. In that case my file was like this

# Root logger option
log4j.rootLogger=ALL, file
# Direct log messages to a log file
log4j.appender.file.File=<my path>admin.log
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

<my path> : The absolute path, where you want to save the admin.log

Now for my purpose I have to edit my main function only (no need to edit my file). I added the following code snippet to my main function.

//Setting log file path
 String dynamicLog = "<my path>" + "admin.log";
 Properties p = new Properties();
 p.load(new FileInputStream("<property file path>\\"));
 p.put( "log4j.appender.file.File", dynamicLog ); // overwrite "log.dir"
 PropertyConfigurator.configure( p );

<my path> : the absolute  path to where I have to save admin.log. ( For me it is now ./inputId/log/admin.log”).

<property file path> : Absolute path to our file.

I tested it, and worked properly. 🙂


Single Post Navigation

One thought on “Dynamic Log File

  1. wow, nice article…keep sharing…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: