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

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 "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:

[handler_hand02]
class=mylogmodule.MyLogHandler
level=DEBUG
formatter=form02
args=('python.log', 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.mylogmodule = mylogmodule

Option 2: Have my module make a copy MyLogHandler class into the
logging (or logging.handlers) 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.handlers module)

My module would include:
import logging
class MyLogHandler(logging.Handler):
...
logging.MyLogHandler = MyLogHandler

The config file would simply have:
class=MyLogHandler
Option 3: Is there an easy (and non-evil) way for me to make my
module available as "logging.mylogmodule" 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__.py"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 6351
On May 28, 9:53 pm, "Lowell Alleman" <low...@allemansonline.com>
wrote:
Here is the situation: I wrote my own log handler class (derived fromlogging.Handler) and I want to be able to use it from aloggingconfig
file, that is, a config file loaded with thelogging.config.fileConfig() function.

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

[handler_hand02]
class=mylogmodule.MyLogHandler
level=DEBUG
formatter=form02
args=('python.log', 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 theloggingmodule's namespace,
before calling fileConfig(). Something like this:

import mylogmodule
importlogging
logging.mylogmodule = mylogmodule

Option 2: Have my module make a copy MyLogHandler class into thelogging(orlogging.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.handlers module)

My module would include:
importlogging
class MyLogHandler(logging.Handler):
...
logging.MyLogHandler = MyLogHandler

The config file would simply have:
class=MyLogHandler

Option 3: Is there an easy (and non-evil) way for me to make my
module available as "logging.mylogmodule" 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__.py"s involved be empty (or have some special
namespace declaration code). The __init__.py for theloggingmodule
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.handlers 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.handlers

class MySpecialHandler(logging.handlers.RotatingFileHand ler):
def __init__(self, fn):
logging.handlers.RotatingFileHandler.__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.MySpecialHandler
level=NOTSET
formatter=form01
args=("rotating.log",)

[formatter_form01]
format=%(asctime)s %(levelname)s %(message)s
datefmt=
class=Formatter

# -- app.py ---
import logging.handlers, logging.config
from myhandler import MySpecialHandler

logging.handlers.MySpecialHandler = MySpecialHandler

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

logger = logging.getLogger("test")

for i in xrange(100):
logger.debug("Message 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.handlers

class MySpecialHandler(logging.handlers.RotatingFileHand ler):
def __init__(self, fn):
logging.handlers.RotatingFileHandler.__init__(self , fn,
maxBytes=2000, backupCount=3)

# Register handler in the "logging.handlers" namespace
logging.handlers.MySpecialHandler = MySpecialHandler
# -- app.py ---
import logging.handlers, logging.config
import myhandler

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

Hi Lowell,

I think it's OK to use the logging.handlers 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.handlers

class MySpecialHandler(logging.handlers.RotatingFileHand ler):
def __init__(self, fn):
logging.handlers.RotatingFileHandler.__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.MySpecialHandler
level=NOTSET
formatter=form01
args=("rotating.log",)

[formatter_form01]
format=%(asctime)s %(levelname)s %(message)s
datefmt=
class=Formatter

# -- app.py ---
import logging.handlers, logging.config
from myhandler import MySpecialHandler

logging.handlers.MySpecialHandler = MySpecialHandler

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

logger = logging.getLogger("test")

for i in xrange(100):
logger.debug("Message 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...@allemansonline.comwrote:
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.handlers

class MySpecialHandler(logging.handlers.RotatingFileHand ler):
def __init__(self, fn):
logging.handlers.RotatingFileHandler.__init__(self , fn,
maxBytes=2000, backupCount=3)

# Register handler in the "logging.handlers" namespacelogging.handlers.MySpecialHandler = MySpecialHandler

# -- app.py ---
importlogging.handlers,logging.config
import myhandler

logging.config.fileConfig("logging.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
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...
0
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...
2
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...
1
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": ...
1
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...
1
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...
1
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...
0
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));...
4
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...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.