473,763 Members | 7,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logging module: problem with custom handlers

I am trying to write a custom logger with a custom handler:

class MyHandler(Strea mHandler):
pass

class Logger:

def __init__(self):
self._l = logging.getLogg er('someident')
self._l.addHand ler(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=MyHandle r" did not work.

Any ideas?

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


you will have to be a tad more specific, i´m 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=MyHandle r" 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(loggi ng.StreamHandle r):
pass

logging.MyHandl er = MyHandler

logging.config. fileConfig("tes t.cfg")
lgr = logging.getLogg er("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.Formatt er() 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
1660
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 and are not documented elsewhere. I used the logging configuration file shown below. My aim was to log only the INFO messages, but log DEBUG messages for one particular module (called webTestLogin.py).
1
3699
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"
23
2224
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 %(function)s, which results in the name of the entity which logged the record. My version even deduces the class name in the case which the logger is a bound method, and assuming the name of the "self" variable is indeed "self".
2
2218
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 but I still get console output on the email logger from the "root" logger. How do I get rid of it? import logging import logging.handlers
0
1257
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 'logging' module is generally working well (even though the documentation makes me miss some proper examples how everything works together) I can't seem to close the log file again to open a new one. This is basically what I'm doing:
7
8586
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 i get these errors: Traceback (most recent call last): File "C:\Python24\lib\logging\handlers.py", line 71, in emit if self.shouldRollover(record):
3
11552
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 logging system will create a default logger that *also* emits logs, which I can't seem to get rid of. Is there a way I can suppress the creation of this default logger, or remove it when I 'm setting up my handlers? Thanks. Sample code:
4
3332
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 a config file like: class=DBHandler
3
6423
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 logging.config.fileConfig() function. Let say my logging class is called "MyLogHandler" and it's in a module called "mylogmodule", I want to be able to make an entry something like this in my logging config file:
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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
6642
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
5270
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.