[Python-Dev] logging class submission
Stephan Stapel
stephan.stapel at web.de
Tue Jan 11 18:26:56 CET 2005
Dear people on the dev list!
I hope that this is the right environment to post my submission request
(I'm new to the scene).
I have modified the RotatingFileHandler of the logging module to create
a daily rolling file handler.
As it works quite good, I would like to suggest inclusion into the
standard logging module of Python. I know that the code is quite trivial
but the class solves the problem of the RotatingFileHandler that you
don't know where to find a certain log entry. By using dates within the
log file name, one can exactly determine which log file to observe when
searching for specific errors.
I hope you like to code and/ or point to improvements on it and finally
move it into the logging module.
cheers,
Stephan
Here comes the code:
# Copyright 2004-2005 by Stephan Stapel <stephan.stapel at web.de>. All
Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Stephan Stapel
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
#
# STEPHAN STAPEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# STEPHAN STAPEL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import logging
from datetime import date
import string
class DailyRollingFileHandler(logging.FileHandler):
"""
The class is based on the standard RotatingFileHandler class from the
official logging module.
It rolls over each day, thus one log file per day is created in the
form
myapp-2005-01-07.log, myapp-2005-01-08.log etc.
"""
def __init__(self, filename, mode="a"):
"""
Open the specified file and use it as the stream for logging.
Rollover occurs whenever the day changes.
The names of the log files each contain the date when they were
created. Thus if the filename "myapp.log" was used, the log files
each have look like myapp-2005-01-07.log etc.
The date is inserted at the position of the last '.' in the
filename
if any or simply appended to the given name if no dot was present.
"""
self.currentDay = date.today()
# create the logfile name parts (base part and extension)
nameparts = string.split(string.strip(filename), ".")
self.filestub = ""
self.fileext = ""
# remove empty items
while nameparts.count("") > 0:
nameparts.remove("")
if len(nameparts) < 2:
self.filestub = nameparts[0]
else:
# construct the filename
for part in nameparts[0:-2]:
self.filestub += part + "."
self.filestub += nameparts[-2]
self.fileext = "." + nameparts[-1]
logging.FileHandler.__init__(self, self.getFilename(), mode)
def getFilename(self):
return self.filestub + "-" + self.currentDay.isoformat() +
self.fileext
def doRollover(self):
"""
Do a rollover, as described in __init__().
"""
self.stream.close()
self.currentDay = date.today()
self.baseFilename = self.getFilename()
self.stream = open(self.baseFilename, "w")
def emit(self, record):
"""
Emit a record.
Output the record to the file, catering for rollover as described
in doRollover().
"""
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2) #due to non-posix-compliant Windows
feature
if date.today() != self.currentDay:
self.doRollover()
logging.FileHandler.emit(self, record)
More information about the Python-Dev
mailing list