RFC: Optional JSON structured logging with `structlog`
Structured logging allows the logs to be machine read and analysed.
There is a python package called structlog
that can be used to implement it. It is available in Debian repositories.
It can be inserted as a formatter for pythons logging
standard library which makes it easy to integrate with lava.
Example:
"json_formatter": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.JSONRenderer(),
"foreign_pre_chain": [
structlog.processors.TimeStamper(fmt="iso"),
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter()
],
},
I was thinking about making it configurable with /etc/lava-server/settings.d/*.yaml
files.
However, first this fix should be merged: !1756 (merged)
Second, YAML can't load actual python objects and logging dict only has one level of special key ()
. I think adding a helper function under absolute python path lava-server.structlog.JsonFormatter
which will initialise the struct log objects. It will also only make imports inside the function which makes entire structlog
optional to be installed.