python - logging, handle missing log file at file rollover -


someone inadvertently moved open log file used python program. program uses logging module timedrotatingfilehandler. when time came roll-over file error 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 file logtest.py, line 16 

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

this reproduces problem (if move log file :))

import time import logging 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" ) in range( 10 ):     time.sleep( 5 )     logger.info( "test logging " + str( ) ) 

my concern here subsequent log messages lost. i'd achieve is, in ascending order of preference:

  1. an exception exits.
  2. an exception can catch , handle.
  3. the logger displays error, subsequent messages go old log file.
  4. the logger displays error, opens new log file , continues normal.

i've skimmed docs/cookbook relevant hooks, nothing's popped out @ me. pointers there equally welcome.

thanks help,

jonathan

exceptions raised in dorollover passed handleerror method of handler. can define subclass , override method whatever want handle error.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -