Yannick's blog

My view on IT

Posts Tagged ‘coding

Dynamic (on the fly) logfiles using log4j

leave a comment »

A few days ago I had to create a logger that would create a file per batch job and per user. After searching the web, I couldn’t find the right solution for this problem. So I would mike toch share with you how this was solved.
First the requirements and pitfalls:
• Project uses log4j
• Implementing without the need for a big refactor in the existing code
• Keep config in existing log4j properties file
• No extra configuration work/deploy/restart of the application server should be needed if a new batch is implemented or a new user starts using the application.
So to solve this I splitted up the problem in two smaller issues, how to let log4j know which batch and user the code is executed? And how to create a log per batch per user.
Since each batch is launched in his own thread I saw the oppertunity to use the Mapped Diagnostic Context (MDC) provided by log4j. Before the job starts we put the userid and job name on the MDC, at the end of the job these are removed from the MDC.


Written by sh33dafi

November 26, 2008 at 22:35

Posted in Coding

Tagged with , ,