diff --git a/lbrynet/analytics/__init__.py b/lbrynet/analytics/__init__.py index 0b39a5e8f..1aafe01b4 100644 --- a/lbrynet/analytics/__init__.py +++ b/lbrynet/analytics/__init__.py @@ -3,4 +3,4 @@ from events import * from api import Api from track import Track from manager import Manager - +from logging_handler import Handler diff --git a/lbrynet/analytics/logging_handler.py b/lbrynet/analytics/logging_handler.py new file mode 100644 index 000000000..fc932e6db --- /dev/null +++ b/lbrynet/analytics/logging_handler.py @@ -0,0 +1,11 @@ +import logging + + +class Handler(logging.Handler): + """A logging handler that reports errors to the analytics manager""" + def __init__(self, manager, level=logging.ERROR): + self.manager = manager + logging.Handler.__init__(self, level) + + def emit(self, record): + self.manager.send_error(record.message) diff --git a/lbrynet/core/log_support.py b/lbrynet/core/log_support.py index 72f96da14..173f6f198 100644 --- a/lbrynet/core/log_support.py +++ b/lbrynet/core/log_support.py @@ -12,6 +12,7 @@ import requests from requests_futures.sessions import FuturesSession import lbrynet +from lbrynet import analytics from lbrynet import conf from lbrynet.core import utils @@ -91,19 +92,24 @@ def _log_decorator(fn): # the numeric level when passed a text level level = logging.getLevelName(level) handler = fn(*args, **kwargs) - if handler.name: - remove_handlers(log, handler.name) - handler.setLevel(level) - log.addHandler(handler) - # need to reduce the logger's level down to the - # handler's level or else the handler won't - # get those messages - if log.level > level: - log.setLevel(level) + configure_handler(handler, log, level) return handler return helper +def configure_handler(handler, log, level): + if handler.name: + remove_handlers(log, handler.name) + handler.setLevel(level) + log.addHandler(handler) + # need to reduce the logger's level down to the + # handler's level or else the handler won't + # get those messages + if log.level > level: + log.setLevel(level) + return handler + + def disable_third_party_loggers(): logging.getLogger('requests').setLevel(logging.WARNING) logging.getLogger('urllib3').setLevel(logging.WARNING) @@ -129,6 +135,12 @@ def configure_file_handler(file_name, **kwargs): return handler +def configure_analytics_handler(analytics_manager): + handler = analytics.Handler(analytics_manager) + handler.name = 'analytics' + return configure_handler(handler, logging.getLogger(), logging.ERROR) + + def get_loggly_url(token=None, version=None): token = token or utils.deobfuscate(conf.settings.LOGGLY_TOKEN) version = version or lbrynet.__version__ diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index fff5aee81..9faeadc47 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -130,6 +130,7 @@ def start_server_and_listen(launchui, use_auth, analytics_manager): analytics_manager: to send analytics """ analytics_manager.send_server_startup() + log_support.configure_analytics_handler(analytics_manager) try: daemon_server = DaemonServer(analytics_manager) yield daemon_server.start(use_auth)