How do I add custom field to Python log format string?

Posted on

Question :

How do I add custom field to Python log format string?

My current format string is:

formatter = logging.Formatter('%(asctime)s : %(message)s')

and I want to add a new field called app_name which will have a different value in each script that contains this formatter.

import logging
formatter = logging.Formatter('%(asctime)s %(app_name)s : %(message)s')

But I’m not sure how to pass that app_name value to the logger to interpolate into the format string. I can obviously get it to appear in the log message by passing it each time but this is messy.

I’ve tried:'Log message', app_name='myapp')'Log message', {'app_name', 'myapp'})'Log message', 'myapp')

but none work.

Answer #1:

You could use a LoggerAdapter so you don’t have to pass the extra info with every logging call:

import logging
extra = {'app_name':'Super App'}

logger = logging.getLogger(__name__)
syslog = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(app_name)s : %(message)s')

logger = logging.LoggerAdapter(logger, extra)'The sky is so blue')

logs (something like)

2013-07-09 17:39:33,596 Super App : The sky is so blue

Filters can also be used to add contextual information.

import logging

class AppFilter(logging.Filter):
    def filter(self, record):
        record.app_name = 'Super App'
        return True

logger = logging.getLogger(__name__)
syslog = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(app_name)s : %(message)s')
logger.addHandler(syslog)'The sky is so blue')

produces a similar log record.

Answered By: unutbu

Answer #2:

You need to pass the dict as a parameter to extra to do it that way.'Log message', extra={'app_name': 'myapp'})


>>> import logging
>>> logging.basicConfig(format="%(foo)s - %(message)s")
>>> logging.warning('test', extra={'foo': 'bar'})
bar - test 

Also, as a note, if you try to log a message without passing the dict, then it will fail.

>>> logging.warning('test')
Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/", line 846, in emit
    msg = self.format(record)
  File "/usr/lib/python2.7/logging/", line 723, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/", line 467, in format
    s = self._fmt % record.__dict__
KeyError: 'foo'
Logged from file <stdin>, line 1
Answered By: mr2ert

Answer #3:


As of Python3.2 you can now use LogRecordFactory

import logging
logging.basicConfig(format="%(custom_attribute)s - %(message)s")
old_factory = logging.getLogRecordFactory()
def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = "my-attr"
    return record

my-attr - hello

Of course, record_factory can be customized to be any callable and the value of custom_attribute could be updated if you keep a reference to the factory callable.

Why is that better than using Adapters / Filters?

  • You do not need to pass your logger around the application
  • It actually works with 3rd party libraries that use their own logger (by just calling logger = logging.getLogger(..)) would now have the same log format. (this is not the case with Filters / Adapters where you need to be using the same logger object)
  • You can stack/chain multiple factories
Answered By: Ahmad

Answer #4:

Another way is to create a custom LoggerAdapter. This is particularly useful when you can’t change the format OR if your format is shared with code that does not send the unique key (in your case app_name):

class LoggerAdapter(logging.LoggerAdapter):
    def __init__(self, logger, prefix):
        super(LoggerAdapter, self).__init__(logger, {})
        self.prefix = prefix

    def process(self, msg, kwargs):
        return '[%s] %s' % (self.prefix, msg), kwargs

And in your code, you would create and initialize your logger as usual:

    logger = logging.getLogger(__name__)
    # Add any custom handlers, formatters for this logger
    myHandler = logging.StreamHandler()
    myFormatter = logging.Formatter('%(asctime)s %(message)s')

Finally, you would create the wrapper adapter to add a prefix as needed:

    logger = LoggerAdapter(logger, 'myapp')'The world bores you when you are cool.')

The output will look something like this:

2013-07-09 17:39:33,596 [myapp] The world bores you when you are cool.
Answered By: rouble

Answer #5:

I found this SO question after implementing it myself. Hope it helps someone. In the code below, I’m inducing an extra key called claim_id in the logger format. It will log the claim_id whenever there is a claim_id key present in the environment. In my use case, I needed to log this information for an AWS Lambda function.

import logging
import os

LOG_FORMAT = '%(asctime)s %(name)s %(levelname)s %(funcName)s %(lineno)s ClaimID: %(claim_id)s: %(message)s'

class AppLogger(logging.Logger):

    # Override all levels similarly - only info overriden here

    def info(self, msg, *args, **kwargs):
        return super(AppLogger, self).info(msg, extra={"claim_id": os.getenv("claim_id", "")})

def get_logger(name):
    """ This function sets log level and log format and then returns the instance of logger"""
    logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
    logger = logging.getLogger(name)
    return logger

LOGGER = get_logger(__name__)"Hey")
os.environ["claim_id"] = "12334""Hey")


Answered By: rhn89

Answer #6:

The accepted answer did not log the format in logfile, whereas the format was reflected in sys output.
Alternatively I used a simpler approach and worked as;

                    format='%(asctime)s: ' +app_name+': %(message)s ',

Answered By: PriyaGp

Answer #7:

Using mr2ert’s answer, I came up with this comfortable solution (Though I guess it’s not recommended) – Override the built-in logging methods to accept the custom argument and create the extra dictionary inside the methods:

import logging

class CustomLogger(logging.Logger):

   def debug(self, msg, foo, *args, **kwargs):
       extra = {'foo': foo}

       if self.isEnabledFor(logging.DEBUG):
            self._log(logging.DEBUG, msg, args, extra=extra, **kwargs)

   *repeat for info, warning, etc*

logger = CustomLogger('CustomLogger', logging.DEBUG)
formatter = logging.Formatter('%(asctime)s [%(foo)s] %(message)s') 
handler = logging.StreamHandler()

logger.debug('test', 'bar')


2019-03-02 20:06:51,998 [bar] test

This is the built in function for reference:

def debug(self, msg, *args, **kwargs):
    Log 'msg % args' with severity 'DEBUG'.

    To pass exception information, use the keyword argument exc_info with
    a true value, e.g.

    logger.debug("Houston, we have a %s", "thorny problem", exc_info=1)
    if self.isEnabledFor(DEBUG):
        self._log(DEBUG, msg, args, **kwargs)
Answered By: Yaniv K.

Answer #8:

import logging;

class LogFilter(logging.Filter):

def __init__(self, code):
    self.code = code

def filter(self, record):
    record.app_code = self.code
    return True

logging.basicConfig(format='[%(asctime)s:%(levelname)s]::[%(module)s -> %(name)s] – APP_CODE:%(app_code)s – MSG:%(message)s’);

class Logger:

def __init__(self, className):
    self.logger = logging.getLogger(className)

def getLogger(className):
    return Logger(className)

def logMessage(self, level, code, msg):

    if level == 'WARN':        
    elif level == 'ERROR':

class Test:
logger = Logger.getLogger(‘Test’)

if __name__=='__main__':
    logger.logMessage('ERROR','123','This is an error')
Answered By: user3672617

Leave a Reply

Your email address will not be published. Required fields are marked *