[IMP] Allow OS to rotate the logs, use WatchedFileHandler if needed.

All this was needed after logrotate conflicted with the TimedRotation
and left the server in a DoS state. Some admins may prefer to handle the
log rotations with logrotate, instead of Python's fascilities.

bzr revid: p_christ@hol.gr-20090709181635-b7j50yaqjl1qsnc1
This commit is contained in:
P. Christeas 2009-07-09 21:16:35 +03:00
parent 2d64ce69d0
commit 56d2cdbd73
2 changed files with 23 additions and 7 deletions

View File

@ -113,7 +113,12 @@ def init_logger():
dirname = os.path.dirname(logf)
if dirname and not os.path.isdir(dirname):
os.makedirs(dirname)
handler = logging.handlers.TimedRotatingFileHandler(logf,'D',1,30)
if tools.config['logrotate'] is not False:
handler = logging.handlers.TimedRotatingFileHandler(logf,'D',1,30)
elif os.name == 'posix':
handler = logging.handlers.WatchedFileHandler(logf)
else:
handler = logging.handlers.FileHandler(logf)
except Exception, ex:
sys.stderr.write("ERROR: couldn't create the logfile directory. Logging to the standard output.\n")
handler = logging.StreamHandler(sys.stdout)
@ -169,11 +174,19 @@ class Logger(object):
result = tools.ustr(msg).strip().split('\n')
except UnicodeDecodeError:
result = msg.strip().split('\n')
if len(result)>1:
for idx, s in enumerate(result):
level_method('[%02d]: %s' % (idx+1, s,))
elif result:
level_method(result[0])
try:
if len(result)>1:
for idx, s in enumerate(result):
level_method('[%02d]: %s' % (idx+1, s,))
elif result:
level_method(result[0])
except IOError,e:
# TODO: perhaps reset the logger streams?
#if logrotate closes our files, we end up here..
pass
except:
# better ignore the exception and carry on..
pass
def shutdown(self):
logging.shutdown()

View File

@ -67,6 +67,7 @@ class configmanager(object):
'import_partial': "",
'pidfile': None,
'logfile': None,
'logrotate': '1',
'smtp_server': 'localhost',
'smtp_user': False,
'smtp_port':25,
@ -129,6 +130,8 @@ class configmanager(object):
# Logging Group
group = optparse.OptionGroup(parser, "Logging Configuration")
group.add_option("--logfile", dest="logfile", help="file where the server log will be stored")
group.add_option("--no-logrotate", dest="logrotate", action="store_false",
default=None, help="do not rotate the logfile")
group.add_option("--syslog", action="store_true", dest="syslog",
default=False, help="Send the log to the syslog server")
group.add_option('--log-level', dest='log_level', type='choice', choices=self._LOGLEVELS.keys(),
@ -229,7 +232,7 @@ class configmanager(object):
self.options[arg] = getattr(opt, arg)
keys = ['language', 'translate_out', 'translate_in', 'debug_mode',
'stop_after_init']
'stop_after_init', 'logrotate']
for arg in keys:
if getattr(opt, arg) is not None: