views:

73

answers:

2

I have an web application written in Java, and I have a thread-pool.

The application is huge, and I cannot make major changes, for example, I cannot change log4j.

I am executing a batch process in the thread pool, and I want to log everything that goes is executed to execute that process.

There will always be just one thread active in the thread pool.

Any ideas of how can I do that?

+1  A: 

I am assuming that everything is executed on the single thread you mention, and by everything I mean everything you are interested in.

If you can change the log4j config file to include thread ids in the log format string, then you can use a tool (e.g. grep) to filter the log to just that thread.

chibacity
I have my own appender. It's a web application so, there are other threads, but I just want to log everything that comes from the batch thread.
Javier
What I am suggesting is that you log everything, but you filter the log after it has been written based on the thread ID of your batch process.
chibacity
+1  A: 

I imagine you have other threads, such as request threads that you don't want to log, and that you want to log just events from the batch to a dedicated logfile.

To write out all logs by the batch to a logfile, you will need to make some runtime tweaks to the log4j config at runtime. You add these changes at the start of the batch, and remove the changes when the batch is complete - the static log4j application config doesn't change, and the runtime log4j config change will not affect the rest of the app.

These are the changes you make to enable logging for just the batch:

  • At the start of the batch, add a new appender to the Cataegory/Logger instances that you want to capure logs from. This could well be the root Category to log all logs regardless of category.
  • The new appender is configured with the current thread (the thread pool thread that the batch is running on). The appender uses this as part of the it's internal filtering to only write out logs from the given thread. The custom appender writes logs by simply delegating to a regular appender, which is writes logs out to wherever you want them.
  • And the end of the batch, remove the logger from the Category (or just disable the appender by setting the logged thread to null.)
mdma
This idea seems interesting. I'll try it. Thanks.
Javier