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
  • I forgot to mention that when I says "log everything" I mean that there are preconfigured loggers in Error and Info modes. I want to print/have/process the debug the debug messages of that loggers. I created my own Appender, but it doesn't works as I expected. Commented Jun 16, 2010 at 22:08

2 Answers 2

1

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.)
Sign up to request clarification or add additional context in comments.

1 Comment

This idea seems interesting. I'll try it. Thanks.
1

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.