Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Passing contextual information when logging
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Vinay Sajip  
View profile   Translate to Translated (View Original)
 More options 8 Jan 2008, 09:08
Newsgroups: comp.lang.python
From: Vinay Sajip <vinay_sa...@yahoo.co.uk>
Date: Tue, 8 Jan 2008 01:08:08 -0800 (PST)
Local: Tues 8 Jan 2008 09:08
Subject: Passing contextual information when logging
Some users of the logging package have raised an issue regarding the
difficulty of passing additional contextual information when logging.
For example, the developer of a networked application may want to log,
in addition to specifics related to to the network service being
provided, information about the IP address of the remote machine and
the username of the person logged into and using the service.

Python 2.4 introduced an 'extra' keyword argument which was intended
to hold a dict-like object containing additional information to be
added to a LogRecord. The additional information would then be printed
via placeholders in a format string.

While this works, it is a little unwieldy to use in practice, because
users need to provide the 'extra' parameter in every logging call.
This has led people in some instances to create context-specific
Logger instances (e.g. one logger for every connection). This has a
drawback in that a logger name can only provide limited contextual
information, and moreover, if the number of connections is effectively
unbounded over time, the number of Logger instances will also grow in
an unbounded way. (Logger instances are never garbage collected,
because references to them are always held in the logging package.
This alleviates a burden on users in that they never have to pass
loggers around, but means that creating a lot of Logger instances will
lead to a long-lived memory burden.)

One solution is to create a generic wrapper around loggers to which a
logger name and contextual information can be passed. The wrapper
would delegate logging calls to the logger with the specified name,
but would manipulate the arguments passed to the logging call to
insert the contextual information. I have created such a wrapper
class, called LoggerAdapter, which is in the example script located at

http://dpaste.com/30230/

I would welcome your views on whether the LoggerAdapter class is
suitable for adding to the logging package in Python 2.6/3.0. Does it
do what might reasonably be expected out of the box? LoggerAdapters
are, of course, garbage collected in the normal way and so impose no
particular memory burden.

Best regards,

Vinay Sajip


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antoine Pitrou  
View profile   Translate to Translated (View Original)
 More options 8 Jan 2008, 09:46
Newsgroups: comp.lang.python
From: Antoine Pitrou <solip...@pitrou.net>
Date: Tue, 8 Jan 2008 09:46:34 +0000 (UTC)
Local: Tues 8 Jan 2008 09:46
Subject: Re: Passing contextual information when logging

Hi Vinay,

> I would welcome your views on whether the LoggerAdapter class is
> suitable for adding to the logging package in Python 2.6/3.0. Does it
> do what might reasonably be expected out of the box?

I think it's quite suited to the problem, yes.

One question : why does the exception() method call Logger.error() rather than
Logger.exception() ?

One suggestion : pass in a Logger object rather than a logger name to the
LoggerAdapter constructor. (this also means that users could stack LoggerAdapter
objects if they wanted to)

Thanks

Antoine.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vinay Sajip  
View profile   Translate to Translated (View Original)
 More options 8 Jan 2008, 14:23
Newsgroups: comp.lang.python
From: Vinay Sajip <vinay_sa...@yahoo.co.uk>
Date: Tue, 8 Jan 2008 06:23:58 -0800 (PST)
Local: Tues 8 Jan 2008 14:23
Subject: Re: Passing contextual information when logging
On 8 Jan, 09:46, Antoine Pitrou <solip...@pitrou.net> wrote:

> One question : why does the exception() method call Logger.error() rather than
> Logger.exception() ?

exception() is a convenience method which adds the keyword argument
exc_info=1 to append traceback information to the log. Both
exception() and error() log at the ERROR level.

> One suggestion : pass in a Logger object rather than a logger name to the
> LoggerAdapter constructor. (this also means that users could stack LoggerAdapter
> objects if they wanted to)

Done, see http://dpaste.com/30253/

Regards,

Vinay


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google