better setup of custom logger class

This commit is contained in:
Jack Robison 2018-11-07 15:23:08 -05:00
parent a6b643bb34
commit bc5824b8bc
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
4 changed files with 14 additions and 12 deletions

View file

@ -102,5 +102,8 @@ class Logger(logging.Logger):
self._log(TRACE, msg, args, **kwargs)
logging.setLoggerClass(Logger)
logging.addLevelName(TRACE, 'TRACE')
def install_logger():
current = logging.getLoggerClass()
if current is not Logger:
logging.setLoggerClass(Logger)
logging.addLevelName(TRACE, 'TRACE')

View file

@ -8,6 +8,7 @@ import twisted.python.log
from twisted.internet import defer
from lbrynet import __version__ as lbrynet_version, build_type
from lbrynet import utils, conf
from lbrynet.custom_logger import install_logger
class HTTPSHandler(logging.Handler):
@ -212,6 +213,8 @@ def configure_logging(file_name, console, verbose=None):
See `convert_verbose` for more details.
"""
logger = logging.getLoggerClass()
if not hasattr(logger, "fail"):
install_logger()
verbose = convert_verbose(verbose)
configure_twisted()
configure_file_handler(file_name)

View file

@ -5,5 +5,3 @@ This includes classes for connecting to other peers and downloading blobs from t
connections from peers and responding to their requests, managing locally stored blobs, sending
and receiving payments, and locating peers in the DHT.
"""
from lbrynet import custom_logger

View file

@ -1,16 +1,14 @@
from io import StringIO
import logging
import mock
import unittest
from unittest import skipIf
from twisted.internet import defer
from twisted import trial
from twisted.trial import unittest
from lbrynet import custom_logger
from tests.util import is_android
from tests.test_utils import is_android
class TestLogger(trial.unittest.TestCase):
class TestLogger(unittest.TestCase):
def raiseError(self):
raise Exception('terrible things happened')
@ -28,7 +26,7 @@ class TestLogger(trial.unittest.TestCase):
handler.setFormatter(logging.Formatter("%(filename)s:%(lineno)d - %(message)s"))
self.log.addHandler(handler)
@unittest.skipIf(is_android(),
@skipIf(is_android(),
'Test cannot pass on Android because the tests package is compiled '
'which results in a different method call stack')
def test_can_log_failure(self):
@ -36,7 +34,7 @@ class TestLogger(trial.unittest.TestCase):
return self.stream.getvalue().split('\n')
# the line number could change if this file gets refactored
expected_first_line = 'test_customLogger.py:20 - My message: terrible things happened'
expected_first_line = 'test_customLogger.py:18 - My message: terrible things happened'
# testing the entirety of the message is futile as the
# traceback will depend on the system the test is being run on