Skip to content

Instantly share code, notes, and snippets.

@dipakc
Created November 6, 2017 23:31
Show Gist options
  • Select an option

  • Save dipakc/c57cb4b5d62c2b40b1483dc70828fe81 to your computer and use it in GitHub Desktop.

Select an option

Save dipakc/c57cb4b5d62c2b40b1483dc70828fe81 to your computer and use it in GitHub Desktop.
Python logging. Using filter
import logging
def foo():
lfoo = logging.getLogger('abc')
lfoo.info('Foo: message')
def bar():
lbar = logging.getLogger('abc')
lbar.info('Bar: message')
if __name__ == '__main__':
l1 = logging.getLogger('abc')
l1.setLevel(logging.INFO)
l1.addHandler(logging.StreamHandler())
foo()
bar()
# How to suppress message in function bar.
###############################
import logging
class MyFilter(logging.Filter): # <<<<<<<<
def filter(self, record):
if record.funcName == 'foo':
return True
else:
return False
def foo():
lfoo = logging.getLogger('abc')
lfoo.info('Foo: message')
def bar():
lbar = logging.getLogger('abc')
lbar.info('Bar: message')
if __name__ == '__main__':
l1 = logging.getLogger('abc')
l1.setLevel(logging.INFO)
l1.addHandler(logging.StreamHandler())
f = MyFilter() # <<<<<<<<
l1.addFilter(f) # <<<<<<<<
foo()
bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment