473,748 Members | 8,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_databas e]
class=DBHandler
level=DEBUG
formatter=datab ase
args=('localhos t', 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.handler s 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 3331
On 15 Oct, 15:15, Frank Aune <Frank.A...@bro adpark.nowrote:
What I'm wondering, is if its possible to specify the database handler in a
config file like:

[handler_databas e]
class=DBHandler
level=DEBUG
formatter=datab ase
args=('localhos t', uid='root')

I've seen the log_test14.py example bundled withloggingmodu le, 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.hand lers 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.custhan dlers = customhandlers # Bind your module to
"custhandle rs" in logging

and then your logging configuration can refer to
"custhandlers.D BHandler". Of course I merely used "custhandle rs" 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=FileHandl er
level=NOTSET
formatter=norma l
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...@bro adpark.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=FileHandl er
level=NOTSET
formatter=norma l
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
2136
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 aims to be easier to use and more powerful. I've developped yet a first alpha that contains several components: File Source
3
1950
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') self._l.addHandler(MyHandler('/path/to/logfile'))
1
3696
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 handlers could be found for logger "root"
0
334
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 ( -1) / 229 closed ( +2) / 452 total ( +1) New / Reopened Patches ______________________
5
6081
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 %(message)s') conerr.setFormatter(conerr_formatter) console = logging.StreamHandler(sys.stdout)
6
1915
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 running. Any tips on the code quality and use of python would be appreciated. I've got a feeling the overall structure is up the creek. approx 220 LOC. file: GodCast.py
2
5113
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 = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
3
2223
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 find any post about this..). My application has two types of components: daemons and regular ones that are run every on crontab. Because the log structure is very similar, I want both to use the same logging configuration file.
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.