forked from LBRYCommunity/lbry-sdk
Remove log handlers with the same name
Also renames functions to follow underscore convention
This commit is contained in:
parent
02a3eab693
commit
622dc87d84
2 changed files with 28 additions and 12 deletions
|
@ -7,18 +7,36 @@ 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):
|
||||
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)
|
||||
handler = fn(*args, **kwargs)
|
||||
if handler.name:
|
||||
remove_handlers(log, handler.name)
|
||||
log.addHandler(handler)
|
||||
log.setLevel(level)
|
||||
return helper
|
||||
|
||||
|
||||
@_log_decorator
|
||||
def configure_console(**kwargs):
|
||||
"""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)
|
||||
handler.name = 'console'
|
||||
return handler
|
||||
|
||||
|
||||
def configureFileHandler(file_name, log=None, level=logging.INFO):
|
||||
log = log or logging.getLogger()
|
||||
@_log_decorator
|
||||
def configure_file_handler(file_name, **kwargs):
|
||||
handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=2097152, backupCount=5)
|
||||
handler.setFormatter(DEFAULT_FORMATTER)
|
||||
log.addHandler(handler)
|
||||
log.setLevel(level=level)
|
||||
handler.name = 'file'
|
||||
return handler
|
||||
|
|
|
@ -74,11 +74,9 @@ def start():
|
|||
parser.set_defaults(branch=False, launchui=True, logtoconsole=False, quiet=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
log_support.configureFileHandler(lbrynet_log)
|
||||
log_support.configure_file_handler(lbrynet_log)
|
||||
if args.logtoconsole:
|
||||
log_support.configureConsole()
|
||||
|
||||
log_support.configure_console()
|
||||
|
||||
try:
|
||||
JSONRPCProxy.from_url(API_CONNECTION_STRING).is_running()
|
||||
|
|
Loading…
Reference in a new issue