2016-12-10 19:33:04 +01:00
|
|
|
import inspect
|
2016-08-08 22:02:36 +02:00
|
|
|
import json
|
2016-07-25 20:04:30 +02:00
|
|
|
import logging
|
|
|
|
import logging.handlers
|
2016-10-22 00:26:36 +02:00
|
|
|
import os
|
2016-07-25 20:04:30 +02:00
|
|
|
import sys
|
2016-08-09 09:01:33 +02:00
|
|
|
import traceback
|
2016-07-20 19:00:34 +02:00
|
|
|
|
2017-03-23 23:25:31 +01:00
|
|
|
from txrequests import Session
|
2017-07-13 20:48:37 +02:00
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
from twisted.internet import defer
|
2017-01-04 05:16:57 +01:00
|
|
|
import twisted.python.log
|
2016-07-20 19:00:34 +02:00
|
|
|
|
2017-04-26 20:15:38 +02:00
|
|
|
from lbrynet import __version__ as lbrynet_version, build_type, conf
|
2016-07-20 19:00:34 +02:00
|
|
|
from lbrynet.core import utils
|
2016-08-09 09:01:33 +02:00
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
####
|
|
|
|
# This code is copied from logging/__init__.py in the python source code
|
|
|
|
####
|
|
|
|
#
|
|
|
|
# _srcfile is used when walking the stack to check when we've got the first
|
|
|
|
# caller stack frame.
|
|
|
|
#
|
2017-03-08 16:21:12 +01:00
|
|
|
if hasattr(sys, 'frozen'): # support for py2exe
|
2016-12-10 19:33:04 +01:00
|
|
|
_srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:])
|
|
|
|
elif __file__[-4:].lower() in ['.pyc', '.pyo']:
|
|
|
|
_srcfile = __file__[:-4] + '.py'
|
|
|
|
else:
|
|
|
|
_srcfile = __file__
|
|
|
|
_srcfile = os.path.normcase(_srcfile)
|
|
|
|
#####
|
|
|
|
|
2016-10-22 00:26:36 +02:00
|
|
|
|
2016-12-14 23:46:06 +01:00
|
|
|
TRACE = 5
|
2016-08-09 09:01:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HTTPSHandler(logging.Handler):
|
2017-03-23 23:25:31 +01:00
|
|
|
def __init__(self, url, fqdn=False, localname=None, facility=None, session=None):
|
2016-08-09 09:01:33 +02:00
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.url = url
|
|
|
|
self.fqdn = fqdn
|
|
|
|
self.localname = localname
|
|
|
|
self.facility = facility
|
2017-03-23 23:25:31 +01:00
|
|
|
self.session = session if session is not None else Session()
|
2016-08-09 09:01:33 +02:00
|
|
|
|
|
|
|
def get_full_message(self, record):
|
|
|
|
if record.exc_info:
|
|
|
|
return '\n'.join(traceback.format_exception(*record.exc_info))
|
|
|
|
else:
|
|
|
|
return record.getMessage()
|
|
|
|
|
2017-07-13 20:48:37 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _emit(self, record):
|
|
|
|
payload = self.format(record)
|
2016-08-09 09:01:33 +02:00
|
|
|
try:
|
2017-07-13 20:48:37 +02:00
|
|
|
yield self.session.post(self.url, data=payload)
|
|
|
|
except ConnectionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
return self._emit(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):
|
2016-10-22 00:26:36 +02:00
|
|
|
"""Configure a logging handler.
|
|
|
|
|
|
|
|
`fn` is a function that returns a logging handler. The returned
|
|
|
|
handler has its log-level set and is attached to the specified
|
|
|
|
logger or the root logger.
|
|
|
|
"""
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
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)
|
2016-12-30 21:31:43 +01:00
|
|
|
configure_handler(handler, log, level)
|
2016-10-22 00:26:36 +02:00
|
|
|
return handler
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
return helper
|
|
|
|
|
|
|
|
|
2016-12-30 21:31:43 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
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-10-30 22:47:37 +01:00
|
|
|
logging.getLogger('lbryum').setLevel(logging.WARNING)
|
2017-01-25 19:04:02 +01:00
|
|
|
logging.getLogger('twisted').setLevel(logging.WARNING)
|
2016-08-08 21:59:30 +02:00
|
|
|
|
2016-08-11 07:14:21 +02:00
|
|
|
|
2016-08-08 21:57:49 +02:00
|
|
|
@_log_decorator
|
|
|
|
def configure_console(**kwargs):
|
2016-10-22 00:26:36 +02:00
|
|
|
"""Convenience function to configure a log-handler that outputs to stdout"""
|
2016-07-25 20:04:30 +02:00
|
|
|
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-10-22 00:26:36 +02:00
|
|
|
"""Convenience function to configure a log-handler that writes to `file_name`"""
|
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):
|
2017-01-17 04:23:20 +01:00
|
|
|
token = token or utils.deobfuscate(conf.settings['LOGGLY_TOKEN'])
|
2017-03-08 16:21:12 +01:00
|
|
|
version = version or lbrynet_version
|
2016-08-08 22:02:36 +02:00
|
|
|
return LOGGLY_URL.format(token=token, tag='lbrynet-' + version)
|
|
|
|
|
|
|
|
|
2017-01-04 05:17:44 +01:00
|
|
|
def configure_loggly_handler(*args, **kwargs):
|
|
|
|
if build_type.BUILD == 'dev':
|
|
|
|
return
|
2017-03-08 16:21:12 +01:00
|
|
|
level = kwargs.pop('level', logging.WARNING)
|
|
|
|
_configure_loggly_handler(*args, level=level, **kwargs)
|
2017-01-04 05:17:44 +01:00
|
|
|
|
|
|
|
|
2016-08-08 22:02:36 +02:00
|
|
|
@_log_decorator
|
2017-01-04 05:17:44 +01:00
|
|
|
def _configure_loggly_handler(url=None, **kwargs):
|
2016-08-08 22:02:36 +02:00
|
|
|
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"""
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-10-26 00:49:35 +02:00
|
|
|
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
|
|
|
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
####
|
|
|
|
# This code is copied from logging/__init__.py in the python source code
|
|
|
|
####
|
|
|
|
def findCaller(srcfile=None):
|
|
|
|
"""Returns the filename, line number and function name of the caller"""
|
|
|
|
srcfile = srcfile or _srcfile
|
|
|
|
f = inspect.currentframe()
|
2017-03-08 16:21:12 +01:00
|
|
|
# On some versions of IronPython, currentframe() returns None if
|
|
|
|
# IronPython isn't run with -X:Frames.
|
2016-12-10 19:33:04 +01:00
|
|
|
if f is not None:
|
|
|
|
f = f.f_back
|
|
|
|
rv = "(unknown file)", 0, "(unknown function)"
|
|
|
|
while hasattr(f, "f_code"):
|
|
|
|
co = f.f_code
|
|
|
|
filename = os.path.normcase(co.co_filename)
|
|
|
|
# ignore any function calls that are in this file
|
|
|
|
if filename == srcfile:
|
|
|
|
f = f.f_back
|
|
|
|
continue
|
|
|
|
rv = (filename, f.f_lineno, co.co_name)
|
|
|
|
break
|
|
|
|
return rv
|
2017-03-08 16:21:12 +01:00
|
|
|
|
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
###
|
|
|
|
|
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(),)
|
2016-10-27 20:28:56 +02:00
|
|
|
exc_info = (failure.type, failure.value, failure.getTracebackObject())
|
|
|
|
log.error(msg, *args, exc_info=exc_info)
|
2016-10-22 00:26:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def convert_verbose(verbose):
|
|
|
|
"""Convert the results of the --verbose flag into a list of logger names
|
|
|
|
|
|
|
|
if --verbose is not provided, args.verbose will be None and logging
|
|
|
|
should be at the info level.
|
|
|
|
if --verbose is provided, but not followed by any arguments, then
|
|
|
|
args.verbose = [] and debug logging should be enabled for all of lbrynet
|
2016-11-15 03:34:56 +01:00
|
|
|
along with info logging on lbryum.
|
2016-10-22 00:26:36 +02:00
|
|
|
if --verbose is provided and followed by arguments, those arguments
|
|
|
|
will be in a list
|
|
|
|
"""
|
|
|
|
if verbose is None:
|
|
|
|
return []
|
|
|
|
if verbose == []:
|
2016-11-15 03:34:56 +01:00
|
|
|
return ['lbrynet', 'lbryum']
|
2016-10-22 00:26:36 +02:00
|
|
|
return verbose
|
|
|
|
|
|
|
|
|
|
|
|
def configure_logging(file_name, console, verbose=None):
|
|
|
|
"""Apply the default logging configuration.
|
|
|
|
|
|
|
|
Enables two log-handlers at the INFO level: a file logger and a loggly logger.
|
|
|
|
Optionally turns on a console logger that defaults to the INFO level, with
|
|
|
|
specified loggers being set to the DEBUG level.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
file_name: the file to which logs should be saved
|
|
|
|
console: If true, enable a console logger
|
|
|
|
verbose: a list of loggers to set to debug level.
|
|
|
|
See `convert_verbose` for more details.
|
|
|
|
"""
|
|
|
|
verbose = convert_verbose(verbose)
|
2017-01-04 05:16:57 +01:00
|
|
|
configure_twisted()
|
2016-10-22 00:26:36 +02:00
|
|
|
configure_file_handler(file_name)
|
|
|
|
configure_loggly_handler()
|
|
|
|
disable_third_party_loggers()
|
|
|
|
if console:
|
|
|
|
# if there are some loggers at the debug level, we need
|
|
|
|
# to enable the console to allow debug. Otherwise, only
|
|
|
|
# allow info.
|
|
|
|
level = 'DEBUG' if verbose else 'INFO'
|
|
|
|
handler = configure_console(level=level)
|
2016-11-15 03:34:56 +01:00
|
|
|
if 'lbryum' in verbose:
|
|
|
|
# TODO: this enables lbryum logging on the other handlers
|
|
|
|
# too which isn't consistent with how verbose logging
|
|
|
|
# happens with other loggers. Should change the configuration
|
|
|
|
# so that its only logging at the INFO level for the console.
|
|
|
|
logging.getLogger('lbryum').setLevel(logging.INFO)
|
|
|
|
verbose.remove('lbryum')
|
2016-10-22 00:26:36 +02:00
|
|
|
if verbose:
|
|
|
|
handler.addFilter(LoggerNameFilter(verbose))
|
|
|
|
|
|
|
|
|
2017-01-04 05:16:57 +01:00
|
|
|
def configure_twisted():
|
|
|
|
"""Setup twisted logging to output events to the python stdlib logger"""
|
|
|
|
# I tried using the new logging api
|
|
|
|
# https://twistedmatrix.com/documents/current/core/howto/logger.html#compatibility-with-standard-library-logging
|
|
|
|
# and it simply didn't work
|
|
|
|
observer = twisted.python.log.PythonLoggingObserver()
|
|
|
|
observer.start()
|
|
|
|
|
|
|
|
|
2016-10-22 00:26:36 +02:00
|
|
|
class LoggerNameFilter(object):
|
|
|
|
"""Filter a log record based on its name.
|
|
|
|
|
|
|
|
Allows all info level and higher records to pass thru.
|
|
|
|
Debug records pass if the log record name (or a parent) match
|
|
|
|
the input list of logger names.
|
|
|
|
"""
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-10-22 00:26:36 +02:00
|
|
|
def __init__(self, logger_names):
|
|
|
|
self.logger_names = logger_names
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
if record.levelno >= logging.INFO:
|
|
|
|
return True
|
|
|
|
name = record.name
|
|
|
|
while name:
|
|
|
|
if name in self.logger_names:
|
|
|
|
return True
|
|
|
|
name = get_parent(name)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_parent(logger_name):
|
|
|
|
names = logger_name.split('.')
|
|
|
|
if len(names) == 1:
|
|
|
|
return ''
|
|
|
|
names = names[:-1]
|
|
|
|
return '.'.join(names)
|
2016-10-22 01:12:38 +02:00
|
|
|
|
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
class Logger(logging.Logger):
|
|
|
|
"""A logger that has an extra `fail` method useful for handling twisted failures."""
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-12-14 02:58:48 +01:00
|
|
|
def fail(self, callback=None, *args, **kwargs):
|
2016-12-10 19:33:04 +01:00
|
|
|
"""Returns a function to log a failure from an errback.
|
|
|
|
|
|
|
|
The returned function appends the error message and extracts
|
|
|
|
the traceback from `err`.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
d.addErrback(log.fail(), 'This is an error message')
|
|
|
|
|
|
|
|
Although odd, making the method call is necessary to extract
|
|
|
|
out useful filename and line number information; otherwise the
|
|
|
|
reported values are from inside twisted's deferred handling
|
|
|
|
code.
|
|
|
|
|
2016-12-10 20:28:32 +01:00
|
|
|
Args:
|
|
|
|
callback: callable to call after making the log. The first argument
|
|
|
|
will be the `err` from the deferred
|
|
|
|
args: extra arguments to pass into `callback`
|
|
|
|
|
|
|
|
Returns: a function that takes the following arguments:
|
2016-12-10 19:33:04 +01:00
|
|
|
err: twisted.python.failure.Failure
|
|
|
|
msg: the message to log, using normal logging string iterpolation.
|
2016-12-10 20:28:32 +01:00
|
|
|
msg_args: the values to subtitute into `msg`
|
2016-12-14 02:58:48 +01:00
|
|
|
msg_kwargs: set `level` to change from the default ERROR severity. Other
|
2016-12-10 19:33:04 +01:00
|
|
|
keywoards are treated as normal log kwargs.
|
|
|
|
"""
|
|
|
|
fn, lno, func = findCaller()
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-12-14 02:58:48 +01:00
|
|
|
def _fail(err, msg, *msg_args, **msg_kwargs):
|
|
|
|
level = msg_kwargs.pop('level', logging.ERROR)
|
2016-12-10 19:33:04 +01:00
|
|
|
msg += ": %s"
|
2016-12-10 20:28:32 +01:00
|
|
|
msg_args += (err.getErrorMessage(),)
|
2016-12-10 19:33:04 +01:00
|
|
|
exc_info = (err.type, err.value, err.getTracebackObject())
|
|
|
|
record = self.makeRecord(
|
2016-12-14 02:58:48 +01:00
|
|
|
self.name, level, fn, lno, msg, msg_args, exc_info, func, msg_kwargs)
|
2016-12-10 19:33:04 +01:00
|
|
|
self.handle(record)
|
2016-12-10 20:28:32 +01:00
|
|
|
if callback:
|
2017-01-18 03:01:04 +01:00
|
|
|
try:
|
|
|
|
return callback(err, *args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
# log.fail is almost always called within an
|
|
|
|
# errback. If callback fails and we didn't catch
|
|
|
|
# the exception we would need to attach a second
|
|
|
|
# errback to deal with that, which we will almost
|
|
|
|
# never do and then we end up with an unhandled
|
|
|
|
# error that will get swallowed by twisted
|
|
|
|
self.exception('Failed to run callback')
|
2017-03-08 16:21:12 +01:00
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
return _fail
|
|
|
|
|
2016-12-14 23:46:06 +01:00
|
|
|
def trace(self, msg, *args, **kwargs):
|
|
|
|
if self.isEnabledFor(TRACE):
|
|
|
|
self._log(TRACE, msg, args, **kwargs)
|
|
|
|
|
2016-12-10 19:33:04 +01:00
|
|
|
|
|
|
|
logging.setLoggerClass(Logger)
|
2016-12-14 23:46:06 +01:00
|
|
|
logging.addLevelName(TRACE, 'TRACE')
|