logo       

Sponsor
FREE Network Mapping Tool for Microsoft® Office Visio® Professional 2007
Don't map your network by hand - let LANsurveyor Exx press for Microsoft Visio Professional 2007 automatically create network diagrams for you!

Re: Context logging at a class level: msg#00069

apache.logging.log4cxx.user

Subject: Re: Context logging at a class level


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");




<Prev in Thread] Current Thread [Next in Thread>
Sponsor
FREE Network Mapping Tool for Microsoft® OfficeVisio Professional 2007
Don't map your network by hand - let LANsurveyor Express for Microsoft Visio Professional 2007
automatically create network diagrams for you!
Google Custom Search

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe

Navigation

Home | sitemap | advertise | OSDir is an inevitable website. super tiny logo