From 622dc87d84843da297be965257a86d0a2f019b9f Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 8 Aug 2016 14:57:49 -0500 Subject: [PATCH] Remove log handlers with the same name Also renames functions to follow underscore convention --- lbrynet/core/log_support.py | 34 ++++++++++++++++----- lbrynet/lbrynet_daemon/LBRYDaemonControl.py | 6 ++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lbrynet/core/log_support.py b/lbrynet/core/log_support.py index 208157d3d..eb1455c0f 100644 --- a/lbrynet/core/log_support.py +++ b/lbrynet/core/log_support.py @@ -7,18 +7,36 @@ DEFAULT_FORMAT = "%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s" DEFAULT_FORMATTER = logging.Formatter(DEFAULT_FORMAT) -def configureConsole(log=None, level=logging.INFO): +def remove_handlers(log, handler_name): + for handler in log.handlers: + if handler.name == handler_name: + log.removeHandler(handler) + + +def _log_decorator(fn): + def helper(*args, **kwargs): + log = kwargs.pop('log', logging.getLogger()) + level = kwargs.pop('level', logging.INFO) + handler = fn(*args, **kwargs) + if handler.name: + remove_handlers(log, handler.name) + log.addHandler(handler) + log.setLevel(level) + return helper + + +@_log_decorator +def configure_console(**kwargs): """Convenience function to configure a logger that outputs to stdout""" - log = log or logging.getLogger() handler = logging.StreamHandler(sys.stdout) handler.setFormatter(DEFAULT_FORMATTER) - log.addHandler(handler) - log.setLevel(level=level) + handler.name = 'console' + return handler -def configureFileHandler(file_name, log=None, level=logging.INFO): - log = log or logging.getLogger() +@_log_decorator +def configure_file_handler(file_name, **kwargs): handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=2097152, backupCount=5) handler.setFormatter(DEFAULT_FORMATTER) - log.addHandler(handler) - log.setLevel(level=level) + handler.name = 'file' + return handler diff --git a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py index 1bfa62eab..9ac321342 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py @@ -74,11 +74,9 @@ def start(): parser.set_defaults(branch=False, launchui=True, logtoconsole=False, quiet=False) args = parser.parse_args() - - log_support.configureFileHandler(lbrynet_log) + log_support.configure_file_handler(lbrynet_log) if args.logtoconsole: - log_support.configureConsole() - + log_support.configure_console() try: JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running()