I want to change the way that the rotating file handler names files.
For example, if I use RotatingFileHandler, it separates the log file when it reaches a specific file size naming "log file name + extension numbering", like below.
filename.log #first log file
filename.log.1 #rotating log file1
filename.log.2 #rotating log file2
However, I want the log handler to name them every time it is created. For example.
09-01-12-20.log #first log file
09-01-12-43.log #rotating log file1
09-01-15-00.log #rotating log file2
How can I do this?
Edit:
I am not asking how to create and name a file.
I want to facilitate python logging
package doing something like inheriting and overriding logging
.
RotatingFileHandler. The RotatingFileHandler class, located in the logging. handlers module, supports rotation of disk log files. class logging.handlers.
Python Logging Handler The log handler is the component that effectively writes/displays a log: Display it in the console (via StreamHandler), in a file (via FileHandler), or even by sending you an email via SMTPHandler, etc. Each log handler has 2 important fields: A formatter which adds context information to a log.
There are two relevant parameters to TimedRotatingFileHandler : when , and interval . Most of the possible values for when , such as D for days, or H for hours, work in conjunction with interval —for example, if when='D', interval=7 were specified, then the log would be rotated every seven days.
I inherit and override RotatingFileHandler
of python logging handler.
RotatingFileHandler has self.baseFilename
value, the handler will use self.baseFilename
to create logFile.(when it creates file first or when rollover happens)
self.shouldRollover()
method, It checks if the handler should rollover logfile or not.
If this method return 1
, it means rollover should happen or return 0
.
By overriding them, I define when this handler makes rollover and which name should be used for new log file by rollover.
-----------------------------------------Edit-----------------------------------------
I post the example code.
from logging import handlers
class DailyRotatingFileHandler(handlers.RotatingFileHandler):
def __init__(self, alias, basedir, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
"""
@summary:
Set self.baseFilename to date string of today.
The handler create logFile named self.baseFilename
"""
self.basedir_ = basedir
self.alias_ = alias
self.baseFilename = self.getBaseFilename()
handlers.RotatingFileHandler.__init__(self, self.baseFilename, mode, maxBytes, backupCount, encoding, delay)
def getBaseFilename(self):
"""
@summary: Return logFile name string formatted to "today.log.alias"
"""
self.today_ = datetime.date.today()
basename_ = self.today_.strftime("%Y-%m-%d") + ".log" + '.' + self.alias_
return os.path.join(self.basedir_, basename_)
def shouldRollover(self, record):
"""
@summary:
Rollover happen
1. When the logFile size is get over maxBytes.
2. When date is changed.
@see: BaseRotatingHandler.emit
"""
if self.stream is None:
self.stream = self._open()
if self.maxBytes > 0 :
msg = "%s\n" % self.format(record)
self.stream.seek(0, 2)
if self.stream.tell() + len(msg) >= self.maxBytes:
return 1
if self.today_ != datetime.date.today():
self.baseFilename = self.getBaseFilename()
return 1
return 0
This DailyRotatingFileHandler will create logfile like
2016-10-05.log.alias
2016-10-05.log.alias.1
2016-10-05.log.alias.2
2016-10-06.log.alias
2016-10-06.log.alias.1
2016-10-07.log.alias.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With