62
63
class DayRotatingFileHandler(TimedRotatingFileHandler):
63
""" A TimedRotatingFileHandler configured for Day rotation but that uses
64
the suffix and extMatch of Hourly rotation, in order to allow seconds based
65
rotation on each startup.
64
"""A mix of TimedRotatingFileHandler and RotatingFileHandler configured for
65
daily rotation but that uses the suffix and extMatch of Hourly rotation, in
66
order to allow seconds based rotation on each startup.
67
The log file is also rotated when the specified size is reached.
68
70
def __init__(self, *args, **kwargs):
69
""" create the instance and override the suffix and extMatch. """
71
""" create the instance and override the suffix and extMatch.
72
Also accepts a maxBytes keyword arg to rotate the file when it reachs
70
75
kwargs['when'] = 'D'
71
76
kwargs['backupCount'] = LOGBACKUP
72
77
# check if we are in 2.5, only for PQM
73
78
if sys.version_info[:2] >= (2, 6):
74
79
kwargs['delay'] = 1
80
if 'maxBytes' in kwargs:
81
self.maxBytes = kwargs.pop('maxBytes')
75
84
TimedRotatingFileHandler.__init__(self, *args, **kwargs)
77
86
self.suffix = "%Y-%m-%d_%H-%M-%S"
78
87
self.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$")
89
def shouldRollover(self, record):
91
Determine if rollover should occur.
93
Basically, see if TimedRotatingFileHandler.shouldRollover and if it's
94
False see if the supplied record would cause the file to exceed
95
the size limit we have.
97
The size based rotation are from logging.handlers.RotatingFileHandler
99
if TimedRotatingFileHandler.shouldRollover(self, record):
103
if self.stream is None: # delay was set...
104
self.stream = self._open()
105
if self.maxBytes > 0: # are we rolling over?
106
msg = "%s\n" % self.format(record)
107
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
108
if self.stream.tell() + len(msg) >= self.maxBytes:
81
113
# pylint: disable-msg=C0103
82
114
class mklog(object):
285
317
# a constant to change the default DEBUG level value
286
318
_DEBUG_LOG_LEVEL = logging.DEBUG
321
# partial config of the handler to rotate when the file size is 1MB
322
CustomRotatingFileHandler = functools.partial(DayRotatingFileHandler,
289
326
root_logger = logging.getLogger("ubuntuone.SyncDaemon")
290
327
root_logger.propagate = False
291
328
root_logger.setLevel(_DEBUG_LOG_LEVEL)
292
root_handler = DayRotatingFileHandler(filename=LOGFILENAME)
329
root_handler = CustomRotatingFileHandler(filename=LOGFILENAME)
293
330
root_handler.addFilter(logging.Filter("ubuntuone.SyncDaemon"))
294
331
root_handler.setFormatter(basic_formatter)
295
332
root_handler.setLevel(_DEBUG_LOG_LEVEL)
296
333
root_logger.addHandler(root_handler)
298
exception_handler = DayRotatingFileHandler(filename=EXLOGFILENAME)
335
exception_handler = CustomRotatingFileHandler(filename=EXLOGFILENAME)
299
336
exception_handler.setFormatter(basic_formatter)
300
337
exception_handler.setLevel(logging.ERROR)
301
338
# add the exception handler to the root logger
310
347
twisted_logger = logging.getLogger('twisted')
311
348
twisted_logger.propagate = False
312
349
twisted_logger.setLevel(logging.ERROR)
313
twisted_handler = DayRotatingFileHandler(filename=LOGFILENAME)
350
twisted_handler = CustomRotatingFileHandler(filename=LOGFILENAME)
314
351
twisted_handler.addFilter(logging.Filter("twisted"))
315
352
twisted_handler.setFormatter(basic_formatter)
316
353
twisted_handler.setLevel(logging.ERROR)
343
380
logfile = os.path.join(LOGFOLDER, 'syncdaemon-debug.log')
344
381
root_handler.baseFilename = os.path.abspath(logfile)
345
382
twisted_handler.baseFilename = os.path.abspath(logfile)
383
# don't cap the file size
384
root_handler.maxBytes = 0
385
twisted_handler.maxBytes = 0
346
386
for name in ['ubuntuone.SyncDaemon', 'twisted']:
347
387
logger = logging.getLogger(name)
348
388
logger.setLevel(_DEBUG_LOG_LEVEL)