Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion hathor/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
29 changes: 25 additions & 4 deletions hathor/cli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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',
},
}
Expand Down