473,721 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom log handler and logging.config. fileConfig()

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 "MyLogHandl er" and it's in a module
called "mylogmodul e", I want to be able to make an entry something
like this in my logging config file:

[handler_hand02]
class=mylogmodu le.MyLogHandler
level=DEBUG
formatter=form0 2
args=('python.l og', 10, True)

I did some digging in the code and documentation, and it doesn't
appear that this question of writing and accessing your own log
handlers is addressed. As per the logging/config.py code, it looks
like the actual file handler classes are being grabbed using an "eval"
from the "logging" module's namespace, like so:
klass = eval(klass, vars(logging))

So this basically means that I have to mess with the "logging"
module's namespace if I want this to work, right? So I've come up
with a couple of options, but I'm trying to figure out what approach
is best:

Option 1: Require the client (the user of my logging module), first
import my module and then copy it into the logging module's namespace,
before calling fileConfig(). Something like this:

import mylogmodule
import logging
logging.mylogmo dule = mylogmodule

Option 2: Have my module make a copy MyLogHandler class into the
logging (or logging.handler s) module, and then let the client use it
from their directly. They client would still have to load my logging
class first (which isn't any different they what they would have to do
if they wanted to use the extended log handlers form the
logging.handler s module)

My module would include:
import logging
class MyLogHandler(lo gging.Handler):
...
logging.MyLogHa ndler = MyLogHandler

The config file would simply have:
class=MyLogHand ler
Option 3: Is there an easy (and non-evil) way for me to make my
module available as "logging.mylogm odule" directly? I am using
setuptools, and it seems like what I've read about "namespaces ", they
do something close to what I'm looking for, but I think that requires
that all of the "__init__.p y"s involved be empty (or have some special
namespace declaration code). The __init__.py for the logging module
is not at all empty, so I suppose that rules out this option? Anyone
have some insights on this?
Thanks in advance,

- Lowell Alleman
Jun 27 '08 #1
3 6422
On May 28, 9:53 pm, "Lowell Alleman" <low...@alleman sonline.com>
wrote:
Here is the situation: I wrote my own log handler class (derived fromlogging.Han dler) and I want to be able to use it from aloggingconfig
file, that is, a config file loaded with thelogging.conf ig.fileConfig() function.

Let say myloggingclass is called "MyLogHandl er" and it's in a module
called "mylogmodul e", I want to be able to make an entry something
like this in myloggingconfig file:

[handler_hand02]
class=mylogmodu le.MyLogHandler
level=DEBUG
formatter=form0 2
args=('python.l og', 10, True)

I did some digging in the code and documentation, and it doesn't
appear that this question of writing and accessing your own log
handlers is addressed. As per thelogging/config.py code, it looks
like the actual file handler classes are being grabbed using an "eval"
from the "logging" module's namespace, like so:
klass = eval(klass, vars(logging))

So this basically means that I have to mess with the "logging"
module's namespace if I want this to work, right? So I've come up
with a couple of options, but I'm trying to figure out what approach
is best:

Option 1: Require the client (the user of myloggingmodule ), first
import my module and then copy it into theloggingmodul e's namespace,
before calling fileConfig(). Something like this:

import mylogmodule
importlogging
logging.mylogmo dule = mylogmodule

Option 2: Have my module make a copy MyLogHandler class into thelogging(orlo gging.handlers) module, and then let the client use it
from their directly. They client would still have to load mylogging
class first (which isn't any different they what they would have to do
if they wanted to use the extended log handlers form thelogging.hand lers module)

My module would include:
importlogging
class MyLogHandler(lo gging.Handler):
...
logging.MyLogHa ndler = MyLogHandler

The config file would simply have:
class=MyLogHand ler

Option 3: Is there an easy (and non-evil) way for me to make my
module available as "logging.mylogm odule" directly? I am using
setuptools, and it seems like what I've read about "namespaces ", they
do something close to what I'm looking for, but I think that requires
that all of the "__init__.p y"s involved be empty (or have some special
namespace declaration code). The __init__.py for theloggingmodul e
is not at all empty, so I suppose that rules out this option? Anyone
have some insights on this?

Thanks in advance,

- Lowell Alleman
Hi Lowell,

I think it's OK to use the logging.handler s namespace to add your
custom handlers - after all, the handlers namespace is for holding
handlers other than the basic ones included in "logging". So...

# -- myhandler.py ---
import logging.handler s

class MySpecialHandle r(logging.handl ers.RotatingFil eHandler):
def __init__(self, fn):
logging.handler s.RotatingFileH andler.__init__ (self, fn,
maxBytes=2000, backupCount=3)
# -- logging.ini ---
[loggers]
keys=root

[handlers]
keys=hand01

[formatters]
keys=form01

[logger_root]
level=NOTSET
handlers=hand01

[handler_hand01]
class=handlers. MySpecialHandle r
level=NOTSET
formatter=form0 1
args=("rotating .log",)

[formatter_form0 1]
format=%(asctim e)s %(levelname)s %(message)s
datefmt=
class=Formatter

# -- app.py ---
import logging.handler s, logging.config
from myhandler import MySpecialHandle r

logging.handler s.MySpecialHand ler = MySpecialHandle r

logging.config. fileConfig("log ging.ini")

logger = logging.getLogg er("test")

for i in xrange(100):
logger.debug("M essage no. %d", i)
should produce the expected results.
Jun 27 '08 #2
Is there any reason not to do this assignment in the "myhandler. py"
directly? This would save a step for each application that needs to
use it.

Starting from your example, it would now look like this:

# -- myhandler.py ---
import logging.handler s

class MySpecialHandle r(logging.handl ers.RotatingFil eHandler):
def __init__(self, fn):
logging.handler s.RotatingFileH andler.__init__ (self, fn,
maxBytes=2000, backupCount=3)

# Register handler in the "logging.handle rs" namespace
logging.handler s.MySpecialHand ler = MySpecialHandle r
# -- app.py ---
import logging.handler s, logging.config
import myhandler

logging.config. fileConfig("log ging.ini")
....

Hi Lowell,

I think it's OK to use the logging.handler s namespace to add your
custom handlers - after all, the handlers namespace is for holding
handlers other than the basic ones included in "logging". So...

# -- myhandler.py ---
import logging.handler s

class MySpecialHandle r(logging.handl ers.RotatingFil eHandler):
def __init__(self, fn):
logging.handler s.RotatingFileH andler.__init__ (self, fn,
maxBytes=2000, backupCount=3)
# -- logging.ini ---
[loggers]
keys=root

[handlers]
keys=hand01

[formatters]
keys=form01

[logger_root]
level=NOTSET
handlers=hand01

[handler_hand01]
class=handlers. MySpecialHandle r
level=NOTSET
formatter=form0 1
args=("rotating .log",)

[formatter_form0 1]
format=%(asctim e)s %(levelname)s %(message)s
datefmt=
class=Formatter

# -- app.py ---
import logging.handler s, logging.config
from myhandler import MySpecialHandle r

logging.handler s.MySpecialHand ler = MySpecialHandle r

logging.config. fileConfig("log ging.ini")

logger = logging.getLogg er("test")

for i in xrange(100):
logger.debug("M essage no. %d", i)
should produce the expected results.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3
On 29 May, 15:53, "Lowell Alleman" <low...@alleman sonline.comwrot e:
Is there any reason not to do this assignment in the "myhandler. py"
directly? This would save a step for each application that needs to
use it.

Starting from your example, it would now look like this:

# -- myhandler.py ---
importlogging.h andlers

class MySpecialHandle r(logging.handl ers.RotatingFil eHandler):
def __init__(self, fn):
logging.handler s.RotatingFileH andler.__init__ (self, fn,
maxBytes=2000, backupCount=3)

# Register handler in the "logging.handle rs" namespaceloggin g.handlers.MySp ecialHandler = MySpecialHandle r

# -- app.py ---
importlogging.h andlers,logging .config
import myhandler

logging.config. fileConfig("log ging.ini")
...
Doing it the way you suggest should be fine.

Regards,

Vinay Sajip
Jun 27 '08 #4

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

Similar topics

12
12722
by: Rob Cranfill | last post by:
Hello, I've successfully coded Python to do what I want with a RotatingFileHandler, but am having trouble getting the same behavior via a config file. I wanted to create one log file each time I run my app, with up to 10 files kept from the last invocations. This was accomplished with self._logger = logging.getLogger('PDGUI')
0
509
by: cgmoore | last post by:
I added the Exception Handling Application Block to a project I am working on and I've created a type that implements IExceptionHandler to serve as a custom handler. When I try to set the TypeName of the custom handler using the Enterprise Library Configuration tool, by attempting to load my project's assembly, it gives me a message "There were no types found in the assembly ... that implement or inherit from the base type ......
2
1784
by: Rob | last post by:
I am developing an intranet application, that has to pass interanl security audit. The framework version that I am using is .Net framework version 1.1. Some combinations of text entered in any text box , involving for example a "less than" character followed by any alpha character, trigger an exception (HttpRequestValidationException). So, the framework detects a potential script injector, triggers an exception and user gets an application...
1
1876
by: Frank S | last post by:
I'm trying to write a custom handler for .rtf files on our website. Using the book "Essential ASP.Net C#" as a guide, I wrote the following and compiled to a dll (assembly named "rtf_cl": using System; using System.Web; using System.Web.SessionState; using System.IO;
1
2015
by: Almad | last post by:
Hi, our applications can have plugins as subpackages and I'd like to allow them to use their own logger as well as it's configuration. I thought that best way will be their own configuration file passed to fileConfig. However, I run into problems... 1) It seems that I cannot refer to something from previously loaded
1
1363
by: ThunderMusic | last post by:
Hi, I want to use a class (if possible other than Socket) which one I could call a custom handler and receive the redirect code it sends... Actually, I tried using the WebClient class, but when the WebClient is redirected, it does not give the redirect code in the result, it automaticaly redirects and put the resulting page into the result... Is there a buit-in class that does what I need in the .Net Framework or will I have to...
1
3646
by: Kenneth Love | last post by:
I have a Python logging config file that contains a RotatingFileHandler handler. In the args key, I have hard-coded the log filename. Everything works great. However, I find that I now need to override this filename at application runtime. Is there a good way to do this? Here is a bit of sample code that (hopefully) clarifies my question. --------------- log.ini -----------------
0
1384
by: Jordan S. | last post by:
Using .NET 3.5... in a "plain old" .aspx page I have the following code in the Init event: this.Context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); this.Context.Response.Cache.SetCacheability(HttpCacheability.Public); this.Context.Response.Cache.SetValidUntilExpires(false); It works great. Testing shows that the page is cached for 60 seconds as expected. Specifically, I insert the current date/time into the Response...
4
2362
by: Matthew Wilson | last post by:
I'm working on a package that uses the standard library logging module along with a .cfg file. In my code, I use logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in the logging config file. However, it seems really obvious to me that this won't work when I share this package with others.
0
8731
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
9067
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
8009
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
6672
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
5986
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
4755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3191
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
2578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2132
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.