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!