473,241 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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
Jan 8 '08 #1
2 1938

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.
Jan 8 '08 #2
On 8 Jan, 09:46, Antoine Pitrou <solip...@pitrou.netwrote:
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
Jan 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Paul | last post by:
I want to use sessions to cover myself in case the user switches off cookies so I am passing the session ID manually through a hidden input field. This is what I have so far. index.php page...
1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
4
by: Dennis M. Marks | last post by:
Has anyone ever considered making a simulated contextual menu. I know that you can sometimes trap a right click or option click. Why not open up a window that would act as a menu when this happens?...
2
by: Squid Seven | last post by:
I have created a class that contains an instance of a second class. When i try to call a member function of the instance of the second class from a member function of the containing class, I get...
3
by: Robert Neville | last post by:
I am looking for JavaScript examples on expanding contextual definition. For example, you have a word like POP3 and the user click on the word expanding it with the following contextual definition....
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
5
by: francois | last post by:
I would like to pass information between ASPX pages. For now, my navigation are hardcoded links. In some case I do a Response.Redirect("pages.aspx") Now I wonder is this is a good way to do. ...
3
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ?...
3
by: hg | last post by:
I have a function called logprintf that take a printf-like syntax. On top of that I want to implement a debug-printf function which calls logprintf but prefix the information using a string: ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.