Uses python's awesome logging-library and configures it to write JSON-lines. In JSON-lines, each line in the log is a valid json-object. This makes the log easy to read with a machine.
pip install json-line-logger
There are three handy constructors for loggers: LoggerFile
,
LoggerStream
, and LoggerStdout
.
import json_line_logger
logger = json_line_logger.LoggerFile(path="test.log.jsonl")
logger.debug("This is a log message")
with json_line_logger.TimeDelta(logger=logger, name="some costly task"):
result_of_a_costly_task = 1 + 1
json_line_logger.shutdown(logger=logger)
Yields a logfile test.log.jsonl
which contains:
{"t":"1886-03-27T11:01:01.341", "l":"DEBUG", "m":"This is a log message"} {"t":"1886-03-27T11:01:01.341", "l":"INFO", "m":"<TimeDelta name='some costly task' delta='2.1457672119140625e-06'/>"}