Hi everyone
Currently most of the logging entries use the "printf-stlye" format for log lines with variables. Like that a custom format could probably not be applied correctly to the log line since the variable is not passed to the formatter.
Example:
I want to format all the log lines as json in my app.
import logging
import logging.handlers
import json
JSON_LOG_FORMAT = '{"severity": "%(levelname)s", "message":"%(message)s"}'
logging.basicConfig(
level=logging.INFO,
format=JSON_LOG_FORMAT
)
class DefaultLoggerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
msg = json.dumps(str(msg))[1:-1] # escape the message as json and cut the last and the first character since it's a quote
return msg, kwargs
log = logging.getLogger("my-app")
log = DefaultLoggerAdapter(log, {})
log.info(' %s', ' "this is my message" ')
With the example code above the output is something like:
{"severity": "INFO", "message": " "this is my message" "}
It does not produce a valid json format in that case, because the log formatter only sees %s instead of "this is my message". Therefore it cannot replace the quotes with \". So the json is expected to look like: {"severity": "INFO", "message": " \"this is my message\" "}
In my case I have to create exceptions of my log-based metrics for pika.
Would you agree to change the log format to something like:
It is the simplest way to circumvent this issue, or do you have an other idea how to fix this?