Skip to content Skip to sidebar Skip to footer

Python Logging - With Json Logs Can I Add An "extra" Value To Every Single Log?

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 w

Solution 1:

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

import logging

classCustomLogger(object):
    def__init__(self, logger_name, log_format, extra=None):
        logging.basicConfig(format=log_format)
        self.logger = logging.getLogger(logger_name)
        self.extra = extra

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

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

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

    deferror(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 importCustomLoggerlogger_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!

Solution 2:

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()
classCustomJsonFormatter(jsonlogger.JsonFormatter):
    defadd_fields(self, log_record, record, message_dict):
        super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
        ifnot 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"}

Post a Comment for "Python Logging - With Json Logs Can I Add An "extra" Value To Every Single Log?"