I want to add a userdefined variable to the logger format using ContextFiltering. My code is as follows :
import logging
import logging.handlers
CMDID='TEST'
class ContextFilter(logging.Filter):
def filter(self,record):
record.CMDID=CMDID
return True
FORMAT='%(asctime)s [%(CMDID)s] - %(message)s'
logger=logging.getLogger("Test")
fh=logger.handlers.RotatingFileHandler("testing.log",maxBytes=1024,backupCount=5)
fh.setlevel(logging.DEBUG)
fh.setFormatter(logging.Formatter(FORMAT))
logger.addHandler(fh)
logger.addFilter(ContextFilter())
logger.warning("WTH")
When I don't use a filehandler and just use a basicConfig for formatting then the code works fine. But on adding a Rotating File Handler I get error
Traceback (most recent call last):
Line 16, in <module>
fh=logger.handlers.RotatingFileHandler("testing.log",maxBytes=1024,backupCount=5)
AttributeError: 'list' object has no attribute 'RotatingFileHandler'
Now I have also tried using LoggerAdapters class for this, but it also throws weird errors which on further investigation I found out that there might be some issues with its documentation.
Can anyone help me regarding this issue and suggest me a way to effectively add contextual user defined info in my logger using this FileHandler.
P.S. I have tried the methods given in the python doc to no use