Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logging Handler

Tags:

python

logging

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

like image 434
DoNNie_DarkO Avatar asked Dec 01 '22 02:12

DoNNie_DarkO


2 Answers

Change logger.handlers. to logging.handlers. and beware of typos (there is no setlevel-method, but a setLevel-method).

The following code works fine:

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")
logger=logging.setLevel(logging.DEBUG)
fh=logging.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")

Explanation: A logger-instance (logger) may have multiple handlers, accessible via it's logger.handlers atrribute. However, you want to instanciate a handler first (via logging.handlers.RotatingFileHandler() ) and use this handler (fh) with your logger (logger.addHandler). Doing this, your logger-instance now is aware of the handler:

In [4]: logger.handlers
Out[4]: [<logging.handlers.RotatingFileHandler at 0x3e85b70>]
like image 172
dorvak Avatar answered Dec 05 '22 14:12

dorvak


This is not completely relevant to the question, but wanted to add to this for total noobs like me because this is the top result when I searched for my problem.

Another reason you can get this issue is if you forget

import logging.handlers
like image 41
Phill Avatar answered Dec 05 '22 13:12

Phill