By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

python logging module and custom handler specified in config file

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.