Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
logging.handlers.RotatingFileHandler Class Reference
Inheritance diagram for logging.handlers.RotatingFileHandler:
Inheritance graph
[legend]
Collaboration diagram for logging.handlers.RotatingFileHandler:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def doRollover
def shouldRollover
def emit

Public Attributes

 maxBytes
 backupCount
 stream
 mode
 encoding

Detailed Description

Handler for logging to a set of files, which switches from one file
to the next when the current file reaches a certain size.

Definition at line 85 of file handlers.py.


Constructor & Destructor Documentation

def logging.handlers.RotatingFileHandler.__init__ (   self,
  filename,
  mode = 'a',
  maxBytes = 0,
  backupCount = 0,
  encoding = None,
  delay = 0 
)
Open the specified file and use it as the stream for logging.

By default, the file grows indefinitely. You can specify particular
values of maxBytes and backupCount to allow the file to rollover at
a predetermined size.

Rollover occurs whenever the current log file is nearly maxBytes in
length. If backupCount is >= 1, the system will successively create
new files with the same pathname as the base file, but with extensions
".1", ".2" etc. appended to it. For example, with a backupCount of 5
and a base file name of "app.log", you would get "app.log",
"app.log.1", "app.log.2", ... through to "app.log.5". The file being
written to is always "app.log" - when it gets filled up, it is closed
and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
exist, then they are renamed to "app.log.2", "app.log.3" etc.
respectively.

If maxBytes is zero, rollover never occurs.

Definition at line 90 of file handlers.py.

00090 
00091     def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
00092         """
00093         Open the specified file and use it as the stream for logging.
00094 
00095         By default, the file grows indefinitely. You can specify particular
00096         values of maxBytes and backupCount to allow the file to rollover at
00097         a predetermined size.
00098 
00099         Rollover occurs whenever the current log file is nearly maxBytes in
00100         length. If backupCount is >= 1, the system will successively create
00101         new files with the same pathname as the base file, but with extensions
00102         ".1", ".2" etc. appended to it. For example, with a backupCount of 5
00103         and a base file name of "app.log", you would get "app.log",
00104         "app.log.1", "app.log.2", ... through to "app.log.5". The file being
00105         written to is always "app.log" - when it gets filled up, it is closed
00106         and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
00107         exist, then they are renamed to "app.log.2", "app.log.3" etc.
00108         respectively.
00109 
00110         If maxBytes is zero, rollover never occurs.
00111         """
00112         # If rotation/rollover is wanted, it doesn't make sense to use another
00113         # mode. If for example 'w' were specified, then if there were multiple
00114         # runs of the calling application, the logs from previous runs would be
00115         # lost if the 'w' is respected, because the log file would be truncated
00116         # on each run.
00117         if maxBytes > 0:
00118             mode = 'a'
00119         BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
00120         self.maxBytes = maxBytes
00121         self.backupCount = backupCount

Here is the caller graph for this function:


Member Function Documentation

Do a rollover, as described in __init__().

Definition at line 122 of file handlers.py.

00122 
00123     def doRollover(self):
00124         """
00125         Do a rollover, as described in __init__().
00126         """
00127         if self.stream:
00128             self.stream.close()
00129             self.stream = None
00130         if self.backupCount > 0:
00131             for i in range(self.backupCount - 1, 0, -1):
00132                 sfn = "%s.%d" % (self.baseFilename, i)
00133                 dfn = "%s.%d" % (self.baseFilename, i + 1)
00134                 if os.path.exists(sfn):
00135                     if os.path.exists(dfn):
00136                         os.remove(dfn)
00137                     os.rename(sfn, dfn)
00138             dfn = self.baseFilename + ".1"
00139             if os.path.exists(dfn):
00140                 os.remove(dfn)
00141             os.rename(self.baseFilename, dfn)
00142         self.mode = 'w'
00143         self.stream = self._open()

Here is the caller graph for this function:

def logging.handlers.BaseRotatingHandler.emit (   self,
  record 
) [inherited]
Emit a record.

Output the record to the file, catering for rollover as described
in doRollover().

Definition at line 69 of file handlers.py.

00069 
00070     def emit(self, record):
00071         """
00072         Emit a record.
00073 
00074         Output the record to the file, catering for rollover as described
00075         in doRollover().
00076         """
00077         try:
00078             if self.shouldRollover(record):
00079                 self.doRollover()
00080             logging.FileHandler.emit(self, record)
00081         except (KeyboardInterrupt, SystemExit):
00082             raise
00083         except:
00084             self.handleError(record)

Here is the call graph for this function:

Determine if rollover should occur.

Basically, see if the supplied record would cause the file to exceed
the size limit we have.

Definition at line 144 of file handlers.py.

00144 
00145     def shouldRollover(self, record):
00146         """
00147         Determine if rollover should occur.
00148 
00149         Basically, see if the supplied record would cause the file to exceed
00150         the size limit we have.
00151         """
00152         if self.stream is None:                 # delay was set...
00153             self.stream = self._open()
00154         if self.maxBytes > 0:                   # are we rolling over?
00155             msg = "%s\n" % self.format(record)
00156             self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
00157             if self.stream.tell() + len(msg) >= self.maxBytes:
00158                 return 1
00159         return 0

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 120 of file handlers.py.

Definition at line 67 of file handlers.py.

Definition at line 119 of file handlers.py.

Reimplemented from logging.handlers.BaseRotatingHandler.

Definition at line 141 of file handlers.py.

Definition at line 128 of file handlers.py.


The documentation for this class was generated from the following file: