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

logging module: problem with custom handlers

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'))
Now I want to configure this logger through a configuration
file. How can I refer to the new handler in the configuration file?
Something like "class=MyHandler" did not work.

Any ideas?

Thanks,
Andreas
Jul 18 '05 #1
3 1919
Andreas Jung <li***@andreas-jung.com> wrote in message news:<ma************************************@pytho n.org>...
I am trying to write a custom logger with a custom handler:


you will have to be a tad more specific, im afraid. what does your
file layout look like? assuming that you have ::

fx.py
class X:
...

fy.py
class Y:
...
y = Y()

then, in order to refer to X, Y, or y from a third file, you can do
something like ::

from fx import X
from fy import Y
from fy import y

or ::

import fx.X

or ::

import fx.X as foo

there. does that help?

_wolf
Jul 18 '05 #2
Andreas Jung wrote:
I am trying to write a custom logger with a custom handler: Now I want to configure this logger through a configuration
file. How can I refer to the new handler in the configuration file?
Something like "class=MyHandler" did not work.

Any ideas?


The handler's class is evaluatated like so in module logging.config:

klass = eval(klass, vars(logging))

For this to succeed you could do something like:
import logging
import logging.config

class MyHandler(logging.StreamHandler):
pass

logging.MyHandler = MyHandler

logging.config.fileConfig("test.cfg")
lgr = logging.getLogger("test")

lgr.info("Hello world")
where the relevant handler section has the line

class=MyHandler

I do not know if there is an "official" way to achieve the same, but then,
why would you build your own Handler in the first place?

Peter
Jul 18 '05 #3
>>>>> "Peter" == Peter Otten <__*******@web.de> writes:

Peter> I do not know if there is an "official" way to achieve the
Peter> same, but then, why would you build your own Handler in the
Peter> first place?

I've just done exactly the same approach (independently), because of a
need to build two Handlers of my own. One is to avoid tracebacks
being logged with a SysLogHandler (it makes the syslog messages too
long, so nothing is logged), and the other is a subclass of
SMTPHandler which changes the subject of the mail if an exception of a
certain type is raised. [1]

I can think of plenty of other instances where building a custom
Handler is needed; for example, an SQLHandler which might log messages
to a database.

[1] This might be better done with a different Formatter, but getting
a custom Formatter to be used by logging.config is much harder than a
custom Handler, due to logging.Formatter() being called from within
logging.config.fileConfig().

Personally I think logging.config.fileConfig() is in need of some
serious refactoring... if only I had time to look into it!

-- Neil
Jul 18 '05 #4

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

Similar topics

0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
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...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
2
by: chuck | last post by:
I want to create two different loggers so that users see one output (e.g. no exception/stack traces) and others (e.g support staff) that does see stack traces via email. The code below is close...
0
by: Christoph Haas | last post by:
Evening, I have an application that is running in an endless loop processing an incoming queue. Every run is supposed to write a log file about the run and then close it again. While the...
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
4
by: Frank Aune | last post by:
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...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
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...
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: 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...
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: 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
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.