madzak / python-json-logger

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

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

Closed MurzNN closed 1 year ago

MurzNN commented 2 years ago

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.

Irvenae commented 2 years ago

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

anesabml commented 2 years ago

@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))
dhaneshsivasamy07 commented 2 years ago

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