I am using python logging.
Once I get a handle to logger object, I do the following to log
logger.info("this is my log message")
My requirement is this:
In the above custom log info message, I would want to pass more parameters like the following:
custom_name = "DISPLAY" logger.info("this is my log message", custom_name)custom_name parameter should be consumed by the log formatter's
custom_nameplaceholder as shown belowformatter = logging.Formatter('%(asctime)s %(custom_name)s:%(name)s %(message)s')
I read articles, stackoverflow posts, python logging documentation. I could only find how to create custom placeholders in the formatter. I am not sure how to pass those custom placeholder values from the log statement itself. Can someone point me to a resource or let me know how to accomplish this ?
Edit:
I could do something like this
import logging
CUSTOM_VAR= 'myCustomVarValue'
class ContextFilter(logging.Filter):
def filter(self, record):
record.CUSTOM_VAR= CUSTOM_VAR
return True
FORMAT = '%(CUSTOM_VAR)s %(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(format=FORMAT, datefmt='%d/%m/%Y %H:%M:%S')
logger = logging.getLogger(__name__)
logger.addFilter(ContextFilter())
logger.info("'this is info message")
The above works fine. However, CUSTOM_VAR is a static value. How can I dynamically pass this value from log.info ??