Logging, processing a missing log file when changing a file

Someone inadvertently moved the open log file used by python. The program uses the registration module with TimedRotatingFileHandler. When it came time to flip the file, this error was thrown:

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 with each subsequent attempt to register something. Recorded messages did not fall 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 is that subsequent log messages are lost. What I would like to achieve, in increasing order of preference:

  • The exception that will be thrown.
  • An exception that I can catch and handle.
  • The logger displays an error, but subsequent messages go to the old log file.
  • The logger displays this error, but opens a new log file and continues to work as usual.

I looked through the documents / cookbooks for the corresponding hooks, but nothing popped up on me. Pointers there are equally welcome.

Thanks for your help,

Jonathan

+4
source share
1 answer

Exceptions raised in doRollover are passed to the handleError method of the handler. You can define a subclass and override this method to do what you want to do to deal with this error.

+1
source

Source: https://habr.com/ru/post/1500580/


All Articles