From 93993e62d6db95f983b015390660060e0a78e3c0 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Mon, 25 Jul 2016 13:04:30 -0500 Subject: [PATCH] Fix: Log configuration should not happen when a module is imported Instead, move the responsibility to the main program. Also, each module had the same, redundant setup. --- lbrynet/core/log_support.py | 24 ++++++++++++++++++++ lbrynet/lbrynet_daemon/LBRYDaemon.py | 20 +++-------------- lbrynet/lbrynet_daemon/LBRYDaemonControl.py | 25 ++++++--------------- lbrynet/lbrynet_daemon/LBRYDaemonServer.py | 5 ++--- lbrynet/lbrynet_daemon/LBRYDownloader.py | 4 +--- lbrynet/lbrynet_daemon/LBRYPublisher.py | 3 --- lbrynet/lbrynet_daemon/LBRYUIManager.py | 5 +---- 7 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 lbrynet/core/log_support.py diff --git a/lbrynet/core/log_support.py b/lbrynet/core/log_support.py new file mode 100644 index 000000000..208157d3d --- /dev/null +++ b/lbrynet/core/log_support.py @@ -0,0 +1,24 @@ +import logging +import logging.handlers +import sys + + +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): + """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) + + +def configureFileHandler(file_name, log=None, level=logging.INFO): + log = log or logging.getLogger() + handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=2097152, backupCount=5) + handler.setFormatter(DEFAULT_FORMATTER) + log.addHandler(handler) + log.setLevel(level=level) diff --git a/lbrynet/lbrynet_daemon/LBRYDaemon.py b/lbrynet/lbrynet_daemon/LBRYDaemon.py index 8811211aa..adcd87055 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemon.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemon.py @@ -56,6 +56,7 @@ from lbrynet.lbryfile.LBRYFileMetadataManager import DBLBRYFileMetadataManager, log = logging.getLogger(__name__) +# TODO: this code snippet is everywhere. Make it go away if sys.platform != "darwin": log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet") else: @@ -68,25 +69,10 @@ lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -# TODO: configuring a logger on module import drastically reduces the -# amount of control the caller of this code has over logging -# -# Better would be to configure all logging at runtime. -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -log.addHandler(handler) -log.setLevel(logging.INFO) - -# if os.path.isfile(lbryum_log): -# f = open(lbryum_log, 'r') -# PREVIOUS_LBRYUM_LOG = len(f.read()) -# f.close() -# else: -# PREVIOUS_LBRYUM_LOG = 0 if os.path.isfile(lbrynet_log): - f = open(lbrynet_log, 'r') - PREVIOUS_LBRYNET_LOG = len(f.read()) - f.close() + with open(lbrynet_log, 'r') as f: + PREVIOUS_LBRYNET_LOG = len(f.read()) else: PREVIOUS_LBRYNET_LOG = 0 diff --git a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py index b25f73a95..1bfa62eab 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemonControl.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemonControl.py @@ -12,10 +12,12 @@ from twisted.web import server from twisted.internet import reactor, defer from jsonrpc.proxy import JSONRPCProxy +from lbrynet.core import log_support from lbrynet.lbrynet_daemon.LBRYDaemonServer import LBRYDaemonServer, LBRYDaemonRequest from lbrynet.conf import API_CONNECTION_STRING, API_INTERFACE, API_ADDRESS, API_PORT, \ DEFAULT_WALLET, UI_ADDRESS, DEFAULT_UI_BRANCH, LOG_FILE_NAME +# TODO: stop it! if sys.platform != "darwin": log_dir = os.path.join(os.path.expanduser("~"), ".lbrynet") else: @@ -25,15 +27,8 @@ if not os.path.isdir(log_dir): os.mkdir(log_dir) lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) - -DEFAULT_FORMAT = "%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s" -DEFAULT_FORMATTER = logging.Formatter(DEFAULT_FORMAT) - log = logging.getLogger(__name__) -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -handler.setFormatter(DEFAULT_FORMATTER) -log.addHandler(handler) -log.setLevel(logging.INFO) + REMOTE_SERVER = "www.google.com" @@ -62,13 +57,6 @@ def stop(): d.callback(None) -def configureConsoleLogger(): - handler = logging.StreamHandler(sys.stdout) - handler.setFormatter(DEFAULT_FORMATTER) - logging.getLogger().addHandler(handler) - logging.getLogger().setLevel(level=logging.INFO) - - def start(): parser = argparse.ArgumentParser(description="Launch lbrynet-daemon") parser.add_argument("--wallet", @@ -86,10 +74,11 @@ def start(): parser.set_defaults(branch=False, launchui=True, logtoconsole=False, quiet=False) args = parser.parse_args() - if args.logtoconsole: - configureConsoleLogger() - args = parser.parse_args() + log_support.configureFileHandler(lbrynet_log) + if args.logtoconsole: + log_support.configureConsole() + try: JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running() diff --git a/lbrynet/lbrynet_daemon/LBRYDaemonServer.py b/lbrynet/lbrynet_daemon/LBRYDaemonServer.py index 1ddaf8d49..3f2107079 100644 --- a/lbrynet/lbrynet_daemon/LBRYDaemonServer.py +++ b/lbrynet/lbrynet_daemon/LBRYDaemonServer.py @@ -20,6 +20,7 @@ from lbrynet.lbrynet_daemon.LBRYDaemon import LBRYDaemon from lbrynet.conf import API_CONNECTION_STRING, API_ADDRESS, DEFAULT_WALLET, UI_ADDRESS, DEFAULT_UI_BRANCH, LOG_FILE_NAME +# TODO: omg, this code is essentially duplicated in LBRYDaemon if sys.platform != "darwin": data_dir = os.path.join(os.path.expanduser("~"), ".lbrynet") else: @@ -29,9 +30,7 @@ if not os.path.isdir(data_dir): lbrynet_log = os.path.join(data_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -log.addHandler(handler) -log.setLevel(logging.INFO) + class LBRYDaemonRequest(server.Request): """ diff --git a/lbrynet/lbrynet_daemon/LBRYDownloader.py b/lbrynet/lbrynet_daemon/LBRYDownloader.py index 9187cc56d..357fb30f1 100644 --- a/lbrynet/lbrynet_daemon/LBRYDownloader.py +++ b/lbrynet/lbrynet_daemon/LBRYDownloader.py @@ -37,9 +37,7 @@ if not os.path.isdir(log_dir): lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -log.addHandler(handler) -log.setLevel(logging.INFO) + class GetStream(object): def __init__(self, sd_identifier, session, wallet, lbry_file_manager, max_key_fee, data_rate=0.5, diff --git a/lbrynet/lbrynet_daemon/LBRYPublisher.py b/lbrynet/lbrynet_daemon/LBRYPublisher.py index bd5c50e4b..2839e46cb 100644 --- a/lbrynet/lbrynet_daemon/LBRYPublisher.py +++ b/lbrynet/lbrynet_daemon/LBRYPublisher.py @@ -24,9 +24,6 @@ if not os.path.isdir(log_dir): lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -log.addHandler(handler) -log.setLevel(logging.INFO) class Publisher(object): diff --git a/lbrynet/lbrynet_daemon/LBRYUIManager.py b/lbrynet/lbrynet_daemon/LBRYUIManager.py index 0f0aa2a53..346a6e37b 100644 --- a/lbrynet/lbrynet_daemon/LBRYUIManager.py +++ b/lbrynet/lbrynet_daemon/LBRYUIManager.py @@ -25,9 +25,6 @@ if not os.path.isdir(log_dir): lbrynet_log = os.path.join(log_dir, LOG_FILE_NAME) log = logging.getLogger(__name__) -handler = logging.handlers.RotatingFileHandler(lbrynet_log, maxBytes=2097152, backupCount=5) -log.addHandler(handler) -log.setLevel(logging.INFO) class LBRYUIManager(object): @@ -239,4 +236,4 @@ class LBRYUIManager(object): def _load_ui(self): for d in [i[0] for i in os.walk(self.active_dir) if os.path.dirname(i[0]) == self.active_dir]: self.root.putChild(os.path.basename(d), static.File(d)) - return defer.succeed(True) \ No newline at end of file + return defer.succeed(True)