From 3afa403a08ff160124f5af1a6f65231a9064d72d Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Thu, 12 Jul 2018 15:00:01 +0800 Subject: [PATCH] Log class name only from server script Based on #518 by eukreign --- electrumx/lib/util.py | 15 +++++++++++++++ electrumx_server | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/electrumx/lib/util.py b/electrumx/lib/util.py index 07826f5..43a8a44 100644 --- a/electrumx/lib/util.py +++ b/electrumx/lib/util.py @@ -36,6 +36,7 @@ import sys from collections import Container, Mapping from struct import pack, Struct +# Logging utilities class ConnectionLogger(logging.LoggerAdapter): '''Prepends a connection identifier to a logging message.''' @@ -44,6 +45,20 @@ class ConnectionLogger(logging.LoggerAdapter): return f'[{conn_id}] {msg}', kwargs +class CompactFormatter(logging.Formatter): + '''Strips the module from the logger name to leave the class only.''' + def format(self, record): + record.name = record.name.rpartition('.')[-1] + return super().format(record) + + +def make_logger(name, *, handler, level): + logger = logging.getLogger(name) + logger.addHandler(handler) + logger.setLevel(logging.INFO) + logger.propagate = False + return logger + # Method decorator. To be used for calculations that will always # deliver the same result. The method cannot take any arguments # and should be accessed as an attribute. diff --git a/electrumx_server b/electrumx_server index fc1e0c1..38b8920 100755 --- a/electrumx_server +++ b/electrumx_server @@ -10,15 +10,20 @@ '''Script to kick off the server.''' import logging +import sys import traceback from electrumx import Controller, Env +from electrumx.lib.util import CompactFormatter, make_logger def main(): '''Set up logging and run the server.''' log_fmt = Env.default('LOG_FORMAT', '%(levelname)s:%(name)s:%(message)s') - logging.basicConfig(level=logging.INFO, format=log_fmt) + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(CompactFormatter(log_fmt)) + make_logger('electrumx', handler=handler, level=logging.INFO) + logging.info('ElectrumX server starting') try: controller = Controller(Env())