python logging - With JSON logs can I add an "extra" value to every single log?
Asked Answered
J

2

6

I'm using the python logging module, along with python-json-logger and I would like to add a few keys:

{
  "app_name": "myapp",
  "env": "prod"
}

To all of my logs automatically without doing the following.

logger.info("Something happened", extra={"app_name": "myapp", "env": "prod"})

But for it to work as if I had. :)

Joijoice answered 1/2, 2019 at 1:29 Comment(0)
P
3

If you are using python-json-logger and have a custom formatter class, you can add those fields directly in log_record in add_fields() method.

from pythonjsonlogger import jsonlogger
from datetime import datetime
import logging
import json


logger = logging.getLogger()

logHandler = logging.StreamHandler()
class CustomJsonFormatter(jsonlogger.JsonFormatter):
    def add_fields(self, log_record, record, message_dict):
        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
        if not log_record.get('datetime'):
            log_record['datetime'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        if log_record.get('level'):
            log_record['level'] = log_record['level'].upper()
        else:
            log_record['level'] = record.levelname
        log_record['app_name']='myapp'
        log_record['env']='prod'

formatter = CustomJsonFormatter('%(datetime)s %(level)s %(message)s')

logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.setLevel(logging.INFO)
logger.info("Something happened.")

This prints out:

{
 "datetime": "2021-12-28T23:43:25.503081Z", 
 "level": "INFO", 
 "message": "Something happened.", 
 "app_name": "myapp", 
 "env": "prod"
}
Proximo answered 28/12, 2021 at 23:46 Comment(1)
how about variables keys, can be use smt like **kwargs?Tacitus
T
2

I would just wrap the main logging class with the custom logic I needed like this:

import logging

class CustomLogger(object):
    def __init__(self, logger_name, log_format, extra=None):
        logging.basicConfig(format=log_format)
        self.logger = logging.getLogger(logger_name)
        self.extra = extra

    def debug(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, extra=self.extra, **kwargs)

    def info(self, msg, *args, **kwargs):
        self.logger.info(msg, *args, extra=self.extra, **kwargs)

    def warning(self, msg, *args, **kwargs):
        self.logger.warning(msg, *args, extra=self.extra, **kwargs)

    def error(self, msg, *args, **kwargs):
        self.logger.error(msg, *args, extra=self.extra, **kwargs)

Then wherever you need a logger create it from that class:

from custom_logging import CustomLogger

logger_name = 'my_logger'       
log_format = '%(asctime)-15s %(app_name)-8s %(env)-8s %(message)s'
extras = {"app_name": "myapp", "env": "prod"}

logger = CustomLogger(logger_name, log_format, extras)
logger.info('Testing it out')

Hope this helps!

Theta answered 1/2, 2019 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.