Log class name only from server script

Based on #518 by eukreign
This commit is contained in:
Neil Booth 2018-07-12 15:00:01 +08:00 committed by Vivek Teega
parent 8beb6c32f6
commit 3afa403a08
2 changed files with 21 additions and 1 deletions

View File

@ -36,6 +36,7 @@ import sys
from collections import Container, Mapping from collections import Container, Mapping
from struct import pack, Struct from struct import pack, Struct
# Logging utilities
class ConnectionLogger(logging.LoggerAdapter): class ConnectionLogger(logging.LoggerAdapter):
'''Prepends a connection identifier to a logging message.''' '''Prepends a connection identifier to a logging message.'''
@ -44,6 +45,20 @@ class ConnectionLogger(logging.LoggerAdapter):
return f'[{conn_id}] {msg}', kwargs 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 # Method decorator. To be used for calculations that will always
# deliver the same result. The method cannot take any arguments # deliver the same result. The method cannot take any arguments
# and should be accessed as an attribute. # and should be accessed as an attribute.

View File

@ -10,15 +10,20 @@
'''Script to kick off the server.''' '''Script to kick off the server.'''
import logging import logging
import sys
import traceback import traceback
from electrumx import Controller, Env from electrumx import Controller, Env
from electrumx.lib.util import CompactFormatter, make_logger
def main(): def main():
'''Set up logging and run the server.''' '''Set up logging and run the server.'''
log_fmt = Env.default('LOG_FORMAT', '%(levelname)s:%(name)s:%(message)s') 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') logging.info('ElectrumX server starting')
try: try:
controller = Controller(Env()) controller = Controller(Env())