We have several jobs that run concurrently that have to use the same config info for log4j. They are all dumping the logs into one file using the same appender. Is there a way to have each job dynamically name its log file so they stay seperate?
Thanks
Tom
You could programmatically configure log4j when you initialize the job.
You can also set the log4j.properties file at runtime via a system property. From the manual:
Assuming you're running the jobs from different java commands, this will enable them to use different log4j.properties files and different filenames for each one.
Without specific knowledge of how your jobs are run it's difficult to say!
log4j.logger.com.foo.admin=,AdminFileAppender log4j.logger.com.foo.report=,ReportFileAppender
It's another way to do this task.. here com.foo.admin is the full package name
We have something similar implemented in our system. We store the specific loggers in a HashMap and initialize appenders for each of them as needed.
Here's an example:
Then to use this in your job you just have to use a one line entry like this:
This will create one log file for each job name and drop it in its own file with that job name in whichever directory you specify.
You can fiddle with other types of appenders and such, as written it will continue appending until the JVM is restarted which may not work if you run the same job on a server that is always up, but this gives the general idea of how it can work.
You can have each job set NDC or MDC and then write an appender that varies the name based on the NDC or MDC value. Creating a new appender isn't too hard. There may also be a appender that will fit the bill in the log4j sandbox. Start looking in http://svn.apache.org/viewvc/logging/log4j/trunk/contribs/
you may implement following:
I may send you some code by mail if you wish...
If the job names are known ahead of time, you could include the job name when you do the getLogger() call. You then can bind different appenders to different loggers, with separate file names (or other destinations).
If you cannot know the job name ahead of time, you could configure the logger at runtime instead of using a configuration file: