diff --git a/hathor/cli/main.py b/hathor/cli/main.py index 32cb80d458..6e0f482913 100644 --- a/hathor/cli/main.py +++ b/hathor/cli/main.py @@ -135,7 +135,11 @@ def execute_from_command_line(self): pudb.set_trace(paused=False) capture_stdout = False - setup_logging(debug, capture_stdout) + json_logs = '--json-logs' in sys.argv + if json_logs: + sys.argv.remove('--json-logs') + + setup_logging(debug, capture_stdout, json_logs) module.main() diff --git a/hathor/cli/util.py b/hathor/cli/util.py index d2f3596e49..590868d077 100644 --- a/hathor/cli/util.py +++ b/hathor/cli/util.py @@ -23,7 +23,13 @@ def create_parser() -> ArgumentParser: return configargparse.ArgumentParser(auto_env_var_prefix='hathor_') -def setup_logging(debug: bool = False, capture_stdout: bool = False, *, _test_logging: bool = False) -> None: +def setup_logging( + debug: bool = False, + capture_stdout: bool = False, + json_logging: bool = False, + *, + _test_logging: bool = False, + ) -> None: import logging import logging.config @@ -142,6 +148,11 @@ def _repr(self, val): else: return super()._repr(val) + if json_logging: + handlers = ['json'] + else: + handlers = ['pretty'] + # See: https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema logging.config.dictConfig({ 'version': 1, @@ -157,13 +168,23 @@ def _repr(self, val): 'processor': ConsoleRenderer(colors=True), 'foreign_pre_chain': pre_chain, }, + 'json': { + '()': structlog.stdlib.ProcessorFormatter, + 'processor': structlog.processors.JSONRenderer(), + 'foreign_pre_chain': pre_chain, + }, }, 'handlers': { - 'default': { + 'pretty': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'colored', }, + 'json': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'json', + }, # 'file': { # 'level': 'DEBUG', # 'class': 'logging.handlers.WatchedFileHandler', @@ -174,12 +195,12 @@ def _repr(self, val): 'loggers': { # set twisted verbosity one level lower than hathor's 'twisted': { - 'handlers': ['default'], + 'handlers': handlers, 'level': 'INFO' if debug else 'WARN', 'propagate': False, }, '': { - 'handlers': ['default'], + 'handlers': handlers, 'level': 'DEBUG' if debug else 'INFO', }, }