Curt Arnold wrote:
> On Dec 17, 2004, at 10:59 AM, Ben Arnold wrote:
> 
>> Say I have a class Agent:
>> 
>> This agent is connected to by a number of IP clients, and there are a
>> number of agents in the system (each with their own ID)
>> 
>> I can use NDC to log the IP of the connecting client for all my
>> logging within the doSomething function, which is good.  However I
>> would also like to include the agent's ID implicitly without having
>> to include it in each call to .info() or .debug() etc.
> 
> You could use MDC to hold the agent ID (it could also hold the IP).
> 
> MDC::set("IP", ip);
> MDC::set("agent", itoa(id()));
> 
> and then use an %X{agent} specification in your layout.

Hi Curt,

  Isn't the MDC thread-specific like the NDC?  I want to be able to have
multiple threads producing the same agent context automatically (though the
IP would be different for each thread.)

> In the current CVS and the forthcoming 0.9.8, you might want
> to consider the stream-like interface provided in
> log4cxx/stream.h.  The code would be like:
> 
> //   not thread safe, so it should not be static
> //   typically created at entry to methods that use it
> logstream agentStream(agentLogger, Level::INFO);
> 
> agentStream << Level::INFO << "Agent " << id() << "
> processing request" << LOG4CXX_ENDMSG;

I noticed the discussion of that a little while ago and like the look of it.
I think I'll be using it as standard when 0.9.8 is ready.

Thanks,
Ben

Reply via email to