I am currently using the python structlog JSONRenderer and hoping to change my log configuration to render the event as the 1st JSON attribute for better readability.
Current Configuration:
structlog.configure(processors=[structlog.processors.JSONRenderer()])
log = structlog.get_logger()
Current log call site:
log.msg("Response: ",
content_type=content_type,
content_length=resp.headers.get('content-length'),
status_code=resp.status_code
)
Current Output:
{"content_type": "application/json", "content_length": null, "status_code": 200, "event": "Response: "}
Desired Output:
{"event": "Response: ", "content_type": "application/json", "content_length": null, "status_code": 200}
Any assistance would be greatly appreciated.
The
structlog.processors.JSONRendererjust passes the log object tojson.dumpsunless you specify another callable instead:The
mydumpswill then be a function that does whatjson.dumpsdoes but putseventfirst. This could look like:What it does is to make a new object then look for
eventkey in the input object and put it first to the new object then proceeding to put rest of keys into the object and pass it along with**kwtojson.dumps.Note that this way you would not need to specify beforehand what other keys your logs might have (like content-type) as any event type might have different info.