Closed MurzNN closed 1 year ago
+1 for this. I need this functionality as well :)
@MurzNN this should do the trick to move timestamp
to the first position in the JSON object:
logger_file.setFormatter(jsonlogger.JsonFormatter('%(timestamp)s %(message)s', timestamp=True))
UPDATE:
To add levelname
to the log output add %(levelname)s
like so:
logger_file.setFormatter(jsonlogger.JsonFormatter('%(timestamp)s %(levelname)s %(message)s ', timestamp=True))
is there any way to set the timestamps with custom values ?
There is this datafmt
parmeter in logger module. It would be great if we can specify the format of the time stamp
logging.Formatter('{"time": "%(asctime)s", "action": "%(message)s"},', datefmt="%Y-%m-%d %H:%M:%S")
I need to have a JSON string in logs, that contains
timestamp
and log level (INFO
,WARNING
, etc), and I want to see timestamp as first element of JSON object. Now I have this code:And it produces this log line in file:
There are two questions:
loglevel = WARNING
key (orlevel
,severity
) to JSON without manually passing it viaextra
?timestamp
at first place in JSON object andloglevel
to start of JSON object?This can be implemented using block of custom code with
CustomJsonFormatter
, but this is a common task and will be good to have a predefined options to add this.