python-json-logger icon indicating copy to clipboard operation
python-json-logger copied to clipboard

Easy way to add log level (levelname) to JSON and move it to first place with timestamp

Open MurzNN opened this issue 4 years ago • 3 comments

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:

  1. How to add loglevel = WARNING key (or level, severity) to JSON without manually passing it via extra?
  2. How to move timestamp at first place in JSON object and loglevel 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.

MurzNN avatar Aug 30 '21 09:08 MurzNN

+1 for this. I need this functionality as well :)

Irvenae avatar Sep 03 '21 15:09 Irvenae

@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))

anesabml avatar Sep 12 '21 09:09 anesabml

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")

dhaneshsivasamy07 avatar Nov 11 '21 06:11 dhaneshsivasamy07