Easy way to add log level (levelname) to JSON and move it to first place with timestamp
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:
logger = logging.getLogger()
logger_file = logging.FileHandler('./backup.log')
logger_file.setFormatter(jsonlogger.JsonFormatter(timestamp=True))
logger.addHandler(logger_file)
logger.warning(f"btrfs_sxbackup_run: Job finished", extra={"duration": 123})
And it produces this log line in file:
{"message": "btrfs_sxbackup_run: Job finished", "duration": 123, "timestamp": "2021-08-30T08:51:22.668213+00:00"}
There are two questions:
- How to add
loglevel = WARNINGkey (orlevel,severity) to JSON without manually passing it viaextra? - How to move
timestampat first place in JSON object andloglevelto 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.
+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")