logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Thomas <greg.d.tho...@gmail.com>
Subject ThreadContext - auto-closable?
Date Fri, 01 Apr 2016 10:16:21 GMT
I was looking at the ThreadContext section of the API @
http://logging.apache.org/log4j/2.x/manual/thread-context.html -
particularly the Thread Context Map

Given it's important to remember to clear the map at the "end" of
processing, would this be a good case for an auto-closeable, so you
could do something like (based on the documented example) ...

try (final CloseableThreadContext ctc = new
CloseableThreadContext("id", UUID.randomUUID().toString(),
"ipAddress", request.getRemoteAddr())) {

    logger.debug("Message 1");
    ...
    logger.debug("Message 2");
    ...
}

i.e. creating the object adds the key/value pairs to the Thread
Context Map, and when the object is auto-closed they are automatically
removed from the Thread Context Map (or returned to their original
value).

Have I missed anything?

(and as I finished writing this, it strikes me that I don't need any
changes to the API, I can do this independently, but it may be worth
adding).

Greg

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message