Home>

I'm currently maintaining the project's python project code,The project uses python's logging module logging, and sets the number of saved logs. But it didn't take effect.Also clean up the data regularly through contab

Foreword

I'm currently maintaining the project's python project code,The project uses python's logging module logging, and sets the number of saved logs. But it didn't take effect.Also clear the data regularly through contab.

analysis

The project uses logging's timedrotatingfilehandler:

#!/user/bin/env python
#-*-coding:utf-8-*-
import logging
from logging.handlers import timedrotatingfilehandler
log=logging.getlogger ()
file_name="./test.log"
logformatter=logging.formatter ("%(asctime) s [%(levelname) s] |%(message) s")
loghandle=timedrotatingfilehandler (file_name, "midnight", 1, 2)
loghandle.setformatter (logformatter)
loghandle.suffix="%y%m%d"
log.addhandler (loghandle)
log.setlevel (logging.debug)
log.debug ("init successful")

Refer to the official documentation of python logging:

Looking at its getting started example, you can see related content using the rotation by time:

import logging
#create logger
logger=logging.getlogger ("simple_example")
logger.setlevel (logging.debug)
#create console handler and set level to debug
ch=logging.streamhandler ()
ch.setlevel (logging.debug)
#create formatter
formatter=logging.formatter ("%(asctime) s-%(name) s-%(levelname) s-%(message) s")
#add formatter to ch
ch.setformatter (formatter)
#add ch to logger
logger.addhandler (ch)
#"application" code
logger.debug ("debug message")

At first glance, there is nothing wrong.

Then look at the recorded code, find the content related to timedrotatingfilehandler,Which deletes the contents of the expired log:

logging/handlers.py

def getfilestodelete (self):
  "" "
  determine the files to delete when rolling over.
  more specific than the earlier method, which just used glob.glob ().
  "" "
  dirname, basename=os.path.split (self.basefilename)
  filenames=os.listdir (dirname)
  result=[]
  prefix=basename + "."
  plen=len (prefix)
  for filename in filenames:
   if filename [:plen] == prefix:
    suffix=filename [plen:]
    if self.extmatch.match (suffix):
     result.append (os.path.join (dirname, filename))
  result.sort ()
  if len (result)<self.backupcount:
   result=[]
  else:
   result=result [:len (result)-self.backupcount]
  return result

The principle of rotation delete,Is found in the log directory,Files that match the suffix suffix,Add to delete list,If it exceeds the specified number, it is added to the list to be deleted.Look at the principle of matching again:

elif self.when == "d" or self.when == "midnight":
   self.interval=60 * 60 * 24 #one day
   self.suffix="%y-%m-%d"
   self.extmatch=r "^ \ d {4}-\ d {2}-\ d {2} $"

exmatch is a regular match,The format is-delimited times,And we set the new suffix ourselves without-separated:

loghandle.suffix="%y%m%d"

So you can't find the file i want to delete,No related logs are deleted.

to sum up

Packaged libraryTry to use public interfaces,Do not modify internal variables at will;

2. The code is problematic,I really can't find the reason.Take a look at the code.

  • Previous Example analysis of JavaScript drag, collision, gravity and elastic motion
  • Next Summary of tips for using UILabel to set fonts in iOS development