From 4d391e7429417dbedef9600b7d7ad8b55acc5d0b Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Tue, 25 Oct 2016 17:49:35 -0500 Subject: [PATCH] Use better json formatting Apply json.dumps at the end of the formatting process instead of the middle. This allows for proper escaping of quotes and allows for actual json to be emitted. --- lbrynet/core/log_support.py | 35 +++++++++++++++++++++----------- lbrynet/lbrynet_daemon/Daemon.py | 1 - 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lbrynet/core/log_support.py b/lbrynet/core/log_support.py index 2f2a5c75b..1fb3979c7 100644 --- a/lbrynet/core/log_support.py +++ b/lbrynet/core/log_support.py @@ -112,19 +112,30 @@ def get_loggly_url(token=None, version=None): @_log_decorator def configure_loggly_handler(url=None, **kwargs): url = url or get_loggly_url() - json_format = { - "loggerName": "%(name)s", - "asciTime": "%(asctime)s", - "fileName": "%(filename)s", - "functionName": "%(funcName)s", - "levelNo": "%(levelno)s", - "lineNo": "%(lineno)d", - "levelName": "%(levelname)s", - "message": "%(message)s", - } - json_format.update(kwargs) - formatter = logging.Formatter(json.dumps(json_format)) + formatter = JsonFormatter(**kwargs) handler = HTTPSHandler(url) handler.setFormatter(formatter) handler.name = 'loggly' return handler + + +class JsonFormatter(logging.Formatter): + """Format log records using json serialization""" + 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) diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index a9fb10f0f..fc67a4132 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -1034,7 +1034,6 @@ class Daemon(jsonrpc.JSONRPC): session_id=self._session_id ) - def _setup_lbry_file_manager(self): self.startup_status = STARTUP_STAGES[3] self.lbry_file_metadata_manager = DBEncryptedFileMetadataManager(self.db_dir)