I think what you want can easily be done with a cobination of the MDC and a
custom filter:

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AppenderSkeleton.html#addFilter(org.apache.log4j.spi.Filter)

1) When your unit of work starts, you put some key/value in the MDC
2) the custom filter denies all request where the specific key/value is not
set
3) when unit of work is done, you remove key from MDC

You don't even have to add appender + filter at runtime, you can do it
initialization.

HTH
Maarten


On 10/31/07, William Damian Faris <[EMAIL PROTECTED]> wrote:
>
> I was wondering if there is a way to do the following that may or may not
> be
> possible.
>
> I have a bunch of code that is already using log4j right now.  The loggers
> that are used to log messages are your standard logfactor.getlog (
> ClassName.class).   I would like to capture all the logging messages for a
> given unit of work into a new appender that is set up at runtime.  I would
> like all the code that runs after setting up this appender to send log
> messages to this new appender as well as the pre-existing configured
> appenders, and I imagine the way to do this is via ThreadId.
>
> Something like this
>
>
> 1. Setup a new appender
> 2. Somehow insert the appender into the log4j configuration tied to the
> current threadID
> 3. call a bunch of old code that is already using log4j, all the log
> messages are caught by the existing configured appenders in addition to my
> new appender
> 4. return from calling code
> 5. Shutdown the new appender and remove it from the existing log4j
> configuration.
>
> So I am asking if its possible to insert an appender such that subsequent
> log messages for a certain threadID go to that appender, and then remove
> that appender
> after code has run.
>
> I will be looking at the docs and code for log4j but thought I would ask
> to
> see if anyone has done anything like this.
>
> Thanks
> Will
>

Reply via email to