diff --git a/bin/netsvc.py b/bin/netsvc.py index 07a6e9c6b71..3ff6416f2b0 100644 --- a/bin/netsvc.py +++ b/bin/netsvc.py @@ -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() diff --git a/bin/tools/config.py b/bin/tools/config.py index 1a07387615f..5376877cb75 100644 --- a/bin/tools/config.py +++ b/bin/tools/config.py @@ -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: