Problem with TimedRotatingFileHandler

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 10 23:16:46 EDT 2008


En Thu, 05 Jun 2008 04:14:07 -0300, <Dominique.Holzwarth at ch.delarue.com>  
escribió:

> Hi everyone
>
> I'm trying to use python's logging mechanism to write exception data  
> into a log file with the TimedRotatingFileHandler but the rotating of  
> the file is not working...
> Here's a bit of sample code of what I'm doing (just the interessting  
> part of it ;-)):
>
> import logging
> import logging.handlers as handlers
> class MyError(Exception):
>
>     fileName = os.path.join(os.path.dirname(__file__), 'Error.log')
>
>     def __init__(self):
>         fileHandler =  
> handlers.TimedRotatingFileHandler(MyError.fileName, when='m',  
> interval=1, backupCount=1)
>         formatter = logging.Formatter('\n%(name)-12s: <%(asctime)s>  
> %(levelname)-8s %(message)s')
>         fileHandler.setFormatter(formatter)
>         logging.getLogger('').addHandler(fileHandler)
>         ## Reference to the logger object
>         self.logger = logging.getLogger('FileLogger')
>         self.logger.setLevel(logging.INFO)
>
> class MyInheritedError(MyError):
>     def __init__(self):
>         MyError.__init__(self)
>         self.logger.error("some stupid text :-)")
>
> The error classes do write into the log file, however there's no  
> rotating. No new file is created (and old ones renamed) nor are there  
> any old log entries deleted/replaced...
> Does anyone have any idea what I could be missing? Might it be a problem  
> due to the fact that these classes inherit from "Exception"?

How do you use that class? raise MyInheritedError(...)? So you're creating  
the handler and logger and all that stuff when the exception is raised?  
Looks really really strange...
Don't inherit from Exception - set up your handlers and loggers at the  
start of the application, then just call getLogger(...).error(some  
message).
A logger called 'FileLogger' is suspicious too - *handlers* determine  
where the log info goes, or how it is processed; *loggers* determine the  
part of the application from which the the message originates (a subsystem  
like "database", or "client.db", or perhaps just the module name).

-- 
Gabriel Genellina




More information about the Python-list mailing list