2016-08-08 22:02:36 +02:00
|
|
|
import json
|
2016-07-25 20:04:30 +02:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
import sys
|
2016-08-09 09:01:33 +02:00
|
|
|
import traceback
|
2016-07-20 19:00:34 +02:00
|
|
|
|
|
|
|
from requests_futures.sessions import FuturesSession
|
|
|
|
|
2016-08-08 22:02:36 +02:00
|
|
|
import lbrynet
|
2016-10-27 21:18:25 +02:00
|
|
|
from lbrynet.conf import settings
|
2016-07-20 19:00:34 +02:00
|
|
|
from lbrynet.core import utils
|
2016-08-09 09:01:33 +02:00
|
|
|
|
|
|
|
session = FuturesSession()
|
|
|
|
|
|
|
|
|
|
|
|
def bg_cb(sess, resp):
|
|
|
|
""" Don't do anything with the response """
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPSHandler(logging.Handler):
|
|
|
|
def __init__(self, url, fqdn=False, localname=None, facility=None):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.url = url
|
|
|
|
self.fqdn = fqdn
|
|
|
|
self.localname = localname
|
|
|
|
self.facility = facility
|
|
|
|
|
|
|
|
def get_full_message(self, record):
|
|
|
|
if record.exc_info:
|
|
|
|
return '\n'.join(traceback.format_exception(*record.exc_info))
|
|
|
|
else:
|
|
|
|
return record.getMessage()
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
try:
|
|
|
|
payload = self.format(record)
|
|
|
|
session.post(self.url, data=payload, background_callback=bg_cb)
|
|
|
|
except (KeyboardInterrupt, SystemExit):
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
self.handleError(record)
|
2016-08-08 22:02:36 +02:00
|
|
|
|
2016-07-25 20:04:30 +02:00
|
|
|
|
|
|
|
DEFAULT_FORMAT = "%(asctime)s %(levelname)-8s %(name)s:%(lineno)d: %(message)s"
|
|
|
|
DEFAULT_FORMATTER = logging.Formatter(DEFAULT_FORMAT)
|
2016-08-08 22:02:36 +02:00
|
|
|
LOGGLY_URL = "https://logs-01.loggly.com/inputs/{token}/tag/{tag}"
|
2016-07-25 20:04:30 +02:00
|
|
|
|
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
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)
|
2016-08-11 07:37:45 +02:00
|
|
|
if not isinstance(level, int):
|
|
|
|
# despite the name, getLevelName returns
|
|
|
|
# the numeric level when passed a text level
|
|
|
|
level = logging.getLevelName(level)
|
2016-08-08 21:57:49 +02:00
|
|
|
handler = fn(*args, **kwargs)
|
|
|
|
if handler.name:
|
|
|
|
remove_handlers(log, handler.name)
|
2016-08-11 07:14:21 +02:00
|
|
|
handler.setLevel(level)
|
2016-08-08 21:57:49 +02:00
|
|
|
log.addHandler(handler)
|
2016-08-11 07:14:21 +02:00
|
|
|
if log.level > level:
|
|
|
|
log.setLevel(level)
|
2016-08-08 21:57:49 +02:00
|
|
|
return helper
|
|
|
|
|
|
|
|
|
2016-08-11 07:14:21 +02:00
|
|
|
def disable_third_party_loggers():
|
2016-08-08 21:59:30 +02:00
|
|
|
logging.getLogger('requests').setLevel(logging.WARNING)
|
2016-10-01 21:20:19 +02:00
|
|
|
logging.getLogger('urllib3').setLevel(logging.WARNING)
|
2016-09-10 08:10:43 +02:00
|
|
|
logging.getLogger('BitcoinRPC').setLevel(logging.INFO)
|
2016-08-08 21:59:30 +02:00
|
|
|
|
2016-08-11 07:14:21 +02:00
|
|
|
def disable_noisy_loggers():
|
2016-08-23 01:56:42 +02:00
|
|
|
logging.getLogger('lbrynet.analytics.api').setLevel(logging.INFO)
|
2016-09-27 19:52:44 +02:00
|
|
|
logging.getLogger('lbrynet.core').setLevel(logging.INFO)
|
2016-08-23 01:56:42 +02:00
|
|
|
logging.getLogger('lbrynet.dht').setLevel(logging.INFO)
|
2016-09-15 05:03:18 +02:00
|
|
|
logging.getLogger('lbrynet.lbrynet_daemon').setLevel(logging.INFO)
|
2016-09-27 20:18:35 +02:00
|
|
|
logging.getLogger('lbrynet.core.Wallet').setLevel(logging.INFO)
|
2016-09-15 05:03:18 +02:00
|
|
|
logging.getLogger('lbrynet.lbryfile').setLevel(logging.INFO)
|
|
|
|
logging.getLogger('lbrynet.lbryfilemanager').setLevel(logging.INFO)
|
2016-08-11 07:14:21 +02:00
|
|
|
|
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
@_log_decorator
|
|
|
|
def configure_console(**kwargs):
|
2016-07-25 20:04:30 +02:00
|
|
|
"""Convenience function to configure a logger that outputs to stdout"""
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
handler.setFormatter(DEFAULT_FORMATTER)
|
2016-08-08 21:57:49 +02:00
|
|
|
handler.name = 'console'
|
|
|
|
return handler
|
2016-07-25 20:04:30 +02:00
|
|
|
|
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
@_log_decorator
|
|
|
|
def configure_file_handler(file_name, **kwargs):
|
2016-07-25 20:04:30 +02:00
|
|
|
handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=2097152, backupCount=5)
|
|
|
|
handler.setFormatter(DEFAULT_FORMATTER)
|
2016-08-08 21:57:49 +02:00
|
|
|
handler.name = 'file'
|
|
|
|
return handler
|
2016-08-08 22:02:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_loggly_url(token=None, version=None):
|
2016-10-19 06:12:44 +02:00
|
|
|
token = token or utils.deobfuscate(settings.LOGGLY_TOKEN)
|
2016-08-08 22:02:36 +02:00
|
|
|
version = version or lbrynet.__version__
|
|
|
|
return LOGGLY_URL.format(token=token, tag='lbrynet-' + version)
|
|
|
|
|
|
|
|
|
|
|
|
@_log_decorator
|
|
|
|
def configure_loggly_handler(url=None, **kwargs):
|
|
|
|
url = url or get_loggly_url()
|
2016-10-26 00:49:35 +02:00
|
|
|
formatter = JsonFormatter(**kwargs)
|
2016-08-09 09:01:33 +02:00
|
|
|
handler = HTTPSHandler(url)
|
2016-08-08 22:02:36 +02:00
|
|
|
handler.setFormatter(formatter)
|
|
|
|
handler.name = 'loggly'
|
|
|
|
return handler
|
2016-10-26 00:49:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
|
|
"""Format log records using json serialization"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.attributes = kwargs
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
data = {
|
|
|
|
'loggerName': record.name,
|
|
|
|
'asciTime': self.formatTime(record),
|
|
|
|
'fileName': record.filename,
|
|
|
|
'functionName': record.funcName,
|
|
|
|
'levelNo': record.levelno,
|
|
|
|
'lineNo': record.lineno,
|
|
|
|
'levelName': record.levelname,
|
|
|
|
'message': record.getMessage(),
|
|
|
|
}
|
|
|
|
data.update(self.attributes)
|
|
|
|
if record.exc_info:
|
|
|
|
data['exc_info'] = self.formatException(record.exc_info)
|
|
|
|
return json.dumps(data)
|
2016-10-25 23:48:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def failure(failure, log, msg, *args):
|
|
|
|
"""Log a failure message from a deferred.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
failure: twisted.python.failure.Failure
|
|
|
|
log: a python logger instance
|
|
|
|
msg: the message to log. Can use normal logging string interpolation.
|
|
|
|
the last argument will be set to the error message from the failure.
|
|
|
|
args: values to substitute into `msg`
|
|
|
|
"""
|
|
|
|
args += (failure.getErrorMessage(),)
|
|
|
|
log.error(msg, *args, exc_info=failure.getTracebackObject())
|