You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Ben Arnold <Be...@magneticnorth.com> on 2004/12/17 17:59:48 UTC

Context logging at a class level

Hi,

I have a (hopefully) simple question about getting context information into
my logging.

Say I have a class Agent:

class Agent { 
public: 
  long id() const { return myId_; } 
  void doSomething();

private:
  static LoggerPtr agentLogger_;
};

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.

So rather than...
agentLogger->info( "Agent " + id() + " processing request" );
(please assume that there is a nice way to concatenate a log4cxx string with
a long, that's merely for ease of reading)

I would rather write
agentLogger->info( "processing request" );

and end up with the same line in my log file, i.e. containing the "Agent 12 "
as context.  This would work together with the NDC to show the agent and
client, so the logging may resemble:

1563 192.168.1.1 Agent 12 processing request

This class-context would have to not be on a thread level as multiple threads
may be accessing several agents for the same client IP.

I had thought of creating an extra member for the Agent class, something like

  std::string loggingContext_;

and a macro to insert this at the front of any actual logging.  Or maybe a
simple class AgentLogger that defers to a static LoggerPtr with context
(this, however, is pretty much the same thing)  Both these seem a bit ugly
however, and I was hoping someone would have a more elegant solution for me.

Thank you all for your time,
Ben

Re: Context logging at a class level

Posted by Curt Arnold <ca...@apache.org>.
On Dec 17, 2004, at 10:59 AM, Ben Arnold wrote:

> Hi,
>
> I have a (hopefully) simple question about getting context information 
> into
> my logging.
>
> Say I have a class Agent:
>
> class Agent {
> public:
>   long id() const { return myId_; }
>   void doSomething();
>
> private:
>   static LoggerPtr agentLogger_;
> };
>
> 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.



>
> So rather than...
> agentLogger->info( "Agent " + id() + " processing request" );
> (please assume that there is a nice way to concatenate a log4cxx 
> string with
> a long, that's merely for ease of reading)

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;


0.9.7 and previous allowed "insertion fragments" to appear in the 
macros, but that is no longer supported:

LOG4CXX_INFO(agentLogger, "Agent " << id() << "processing request");