madzak / python-json-logger

Json Formatter for the standard python logger
BSD 2-Clause "Simplified" License
1.7k stars 231 forks source link

Change format of message to Message #158

Closed sharathsridhar closed 1 year ago

sharathsridhar commented 1 year ago

Hello, this isn't an issue, but wanted to know if there was an easier way to call Message instead of message or do we have to go the CustomJsonFormatter class route? For example, how to change the message here

formatter = jsonlogger.JsonFormatter("%(asctime)s %(filename)s %(module)s %(funcName)s %(lineno)s %(message)s")

to Message, because changing it directly in this line of code wouldn't work?

Thank you.

madzak commented 1 year ago

Obviously late, but hey maybe it'll help.

Your code above is defining how the output should be so changing %(message)s to %(Message)s will cause message to not appear in the JSON output.

If you want to rename the field, you can see how we do that here: https://github.com/madzak/python-json-logger/blob/master/tests/test_jsonlogger.py#L55

Basically you add rename_fields={'message': 'Message'} to the JsonFormatter call:

formatter = jsonlogger.JsonFormatter("%(asctime)s %(filename)s %(module)s %(funcName)s %(lineno)s %(message)s", rename_fields={'message': 'Message'})