Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging, handle missing log file at file rollover

Tags:

python

logging

Someone inadvertently moved the open log file used by a python program. The program uses the logging module with a TimedRotatingFileHandler. When the time came to roll-over the file this error was output:

Traceback (most recent call last):
File "/python_root/lib/logging/handlers.py", line 78, in emit
    self.doRollover()
  File "/python_root/lib/logging/handlers.py", line 338, in doRollover
    os.rename(self.baseFilename, dfn)
OSError: [Errno 2] no such file or directory
Logged from file logtest.py, line 16

The error was repeated on each subsequent attempt to log something. The logged messages did not go into the old (moved) log file.

This reproduces the problem (if you move the log file :))

import time
import logging
from logging import handlers

f = logging.Formatter( "%(asctime)s %(message)s" )
h = handlers.TimedRotatingFileHandler(
      "testlog", when='s', interval=5, backupCount=10 )
h.setFormatter( f )
logger = logging.getLogger( 'test' )
logger.setLevel( logging.INFO )
logger.addHandler( h )
logger.info( "LOGTEST started" )
for i in range( 10 ):
    time.sleep( 5 )
    logger.info( "Test logging " + str( i ) )

My concern here is that subsequent log messages are lost. What I'd like to achieve is, in ascending order of preference:

  1. An exception that exits.
  2. An exception I can catch and handle.
  3. The logger displays the error, but subsequent messages go to the old log file.
  4. The logger displays this error, but opens a new log file and continues as normal.

I've skimmed the docs/cookbook for relevant hooks, but nothing's popped out at me. Pointers there are equally welcome.

Thanks for your help,

Jonathan

like image 407
turnstone Avatar asked Nov 12 '22 22:11

turnstone


1 Answers

Exceptions that are raised in doRollover are passed to the handleError method of the handler. You can define a subclass and override this method to do whatever it is you want to do to handle the error.

like image 100
Vinay Sajip Avatar answered Nov 14 '22 21:11

Vinay Sajip