2

Can I use ContextFilters in python logging to change loglevels based on modulenames. So something like

class ContextFilter(logging.Filter):
  CMDID_cf="IAMTEST1"
  def __init__(self, CMDID1): 
  self.CMDID_cf=CMDID1

  def filter(self,record):
    record.CMDID=self.CMDID_cf
    return True

  def moduleFilter(self):
     //something like below//
     if(modulename=="something")
     setLevel(logging.ERROR)

  #MAIN
  **define logger**
  obj=ContextFilter()
  logging.addFilter(obj.modukeFilter)

In the python documentation I haven't really found out anything about this so any help pointing to this would be great.

1 Answer 1

2

The appropriate way, which is documented here, is to use module-level loggers named with __name__ and to then set their levels as desired in the logging configuration. There's no need to use a Filter for this.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Vinay,I went through your complete documentation and I agree that its the best way to implement. But due to the existing stupid structure of logging in my application (which obviously I cant change and its quite extensively used throughout) I was trying to find out an alternative method. Please help me if the method I am suggesting to use leads me anywhere towards a good solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.