473,287 Members | 1,515 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,287 software developers and data experts.

python logging module and custom handler specified in config file

Hello,

I've been playing with the python logging module lately, and I manage to log
to both stderr and MySQL database.

What I'm wondering, is if its possible to specify the database handler in a
config file like:

[handler_database]
class=DBHandler
level=DEBUG
formatter=database
args=('localhost', uid='root')

I've seen the log_test14.py example bundled with logging module, which
describes the DBHandler class and I can get it working if I attach this
handler to the root logger inside my application, but I would really like to
do it through a config file like above. But since the logging.handlers module
does not know about the DBHandler class, obviously this does not work.

I was thinking perhaps specifying module and classname in the class= option
above, like class=dbhandler.DBHandler, but it will just complain that
name 'dbhandler' is not defined.

Is this possible to archieve somehow?

Best regards,
Frank Aune
Oct 15 '07 #1
4 3289
On 15 Oct, 15:15, Frank Aune <Frank.A...@broadpark.nowrote:
What I'm wondering, is if its possible to specify the database handler in a
config file like:

[handler_database]
class=DBHandler
level=DEBUG
formatter=database
args=('localhost', uid='root')

I've seen the log_test14.py example bundled withloggingmodule, which
describes the DBHandler class and I can get it working if I attach this
handler to the root logger inside my application, but I would really like to
do it through a config file like above. But since thelogging.handlers module
does not know about the DBHandler class, obviously this does not work.

I was thinking perhaps specifying module and classname in the class= option
above, like class=dbhandler.DBHandler, but it will just complain that
name 'dbhandler' is not defined.

Is this possible to archieve somehow?
The values in the config file are interpreted in the context of the
logging module's namespace. Hence, one way of achieving what you want
is putting any custom handlers in
a module of your own, and providing a binding in the logging module's
namespace. For example: assuming your DBHandler is defined in a module
customhandlers, you could do this somewhere in your code, before
loading the configuration:

import logging
import customhandlers # Use your own module name here

logging.custhandlers = customhandlers # Bind your module to
"custhandlers" in logging

and then your logging configuration can refer to
"custhandlers.DBHandler". Of course I merely used "custhandlers" and
"customhandlers" to show how you can bind to an arbitrary name.

The same technique applies to the arguments passed to the constructor.

Hope this helps,

Vinay Sajip
Oct 18 '07 #2
On Thursday 18 October 2007 19:26:59 Vinay Sajip wrote:
The same technique applies to the arguments passed to the constructor.

Hope this helps,
Thank you! This seems like exactly what I was looking for.

Very impressive logging framework btw - I use it whenever I can =)

Cheers,
Frank
Oct 18 '07 #3
On Thursday 18 October 2007 19:26:59 Vinay Sajip wrote:
The values in the config file are interpreted in the context of the
logging module's namespace. Hence, one way of achieving what you want
is putting any custom handlers in
a module of your own, and providing a binding in the logging module's
namespace.
What if you want to datestamp filenames for filehandlers, say with todays date
for example?

[handler_file]
class=FileHandler
level=NOTSET
formatter=normal
args=('filename.log', 'w')

Say you want filenames to be on format filename-YYYYMMDD.log

Thanks,
Frank
Oct 19 '07 #4
On 19 Oct, 12:04, Frank Aune <Frank.A...@broadpark.nowrote:
On Thursday 18 October 2007 19:26:59 Vinay Sajip wrote:

What if you want to datestamp filenames for filehandlers, say with todays date
for example?

[handler_file]
class=FileHandler
level=NOTSET
formatter=normal
args=('filename.log', 'w')

Say you want filenames to be on format filename-YYYYMMDD.log
You can use ConfigParser interpolation. See this post by Gabriel
Genellina:

http://groups.google.co.uk/group/com...2cca545d8b339c

Regards,
Vinay

Oct 19 '07 #5

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

Similar topics

4
by: Alexander DEJANOVSKI | last post by:
Hi all, I'm starting a new project to develop an Open-Source EAI server in Python and I'm looking for motivated developpers and testers. It is inspired by Open Adaptor (www.openadaptor.org), but...
3
by: Andreas Jung | last post by:
I am trying to write a custom logger with a custom handler: class MyHandler(StreamHandler): pass class Logger: def __init__(self): self._l = logging.getLogger('someident')...
1
by: j vickroy | last post by:
My system: MSW XP professional Python 2.3.3 logging package: 0.4.9.2 My problem: The log_test3.py script, provided with the logging package distribution, generates an unexpected message: No...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
5
by: Ritesh Raj Sarraf | last post by:
import os, sys, logging logger = logging.getLogger("my_app") conerr = logging.StreamHandler(sys.stderr) conerr.setLevel(logging.DEBUG) conerr_formatter = logging.Formatter('%(levelname)s...
6
by: Lex Hider | last post by:
Hi, Apologies if this is against etiquette. I've just got my first python app up and running. It is a podcast aggregator depending on feedparser. I've really only learnt enough to get this up and...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
3
by: guybenron | last post by:
Hey, I have a sort of petty-neurotic question, I'm kinda pedantic and like to keep my log directories clean, and this thing is bothering me to the point of actually posting a question (couldn't...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.