Python logging.Filter()














































Python logging.Filter()



                                        logging.Filter()

Description:
                    -logging.Filter(name) method returns an instance of Filter class. if the name is specified, its names logger which, together with its children, will have its events allowed through the filter.
                          - if we want to allow only specific logger messages then we can apply a filter.
                          - if the name is the empty string, allows every event.it is better to declare a filter when a need.

Signature:
                   Filter(name)
where,
name - logger name hierarchy.

Program:
                 The Engineering package contains a computer package that contains pythondev and javadev as two modules. In MBBS package contain a doctor module.

  
1.pythondev

import logging

logger = logging.getLogger(__name__)

def getLogMessage():
    logger.info('message from Engineering-->computer-->pythondev')



2.javadev

import logging

logger = logging.getLogger(__name__)

def getLogMessage():
    logger.info('work from home')
    logger.info('message from Engineering-->computer-->javadev')


doctor.py

import logging

logger = logging.getLogger(__name__)

def getLogMessage():
    logger.warning('Be safe from corono')


Now consider the University module


University.py

import logging
import sys

from Engineering.computer import pythondev
from Engineering.computer import javadev
from MBBS import doctor

#create logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

# create stream handle
streamhdlr = logging.StreamHandler(sys.stdout)
filehdlr = logging.FileHandler('filter.txt',mode='w')

# set level 
streamhdlr.setLevel(logging.INFO)
filehdlr.setLevel(logging.INFO)

# add handler
logger.addHandler(streamhdlr)
logger.addHandler(filehdlr)

#create filter
fltr = logging.Filter('Engineering')

# add filter to handler
streamhdlr.addFilter(fltr)
filehdlr.addFilter(fltr)

pythondev.getLogMessage()
javadev.getLogMessage()
doctor.getLogMessage()

output:

message from Engineering-->computer-->pythondev
work from home
message from Engineering-->computer-->javadev



filter.txt:

message from Engineering-->computer-->pythondev
work from home
message from Engineering-->computer-->javadev


we can add a filter handler and logger.

 

More Articles of AARTI SHELAR:

Name Views Likes
Python logging.Handler.removeFilter() 782 0
Python BeautifulSoup.Tag.find_next_sibling() and BeautifulSoup.Tag.find_next_siblings() 4159 0
Python logging method of logger object 346 0
Beautiful Soup Navigating Parse Tree by Going Sideways 548 1
Python logging.getLogger().removeHandler(handler) 5449 0
Python logging.Handler.setFormatter() 3437 0
Python logging.getLogger().getChild(suffix) 624 0
python logging.getLogger().addHandler(handler) 3795 0
Beautiful Soup Navigating Parse Tree by Going Down 317 0
Python logging.Filter() 590 0
Beautiful Soup Navigating Parse Tree by Going Back and Forth 317 0
Python logging.Formatter() 602 0
Beautiful Soup Navigating Parse Tree by Going Up 326 0
Python BeautifulSoup.Tag.find_next() and BeautifulSoup.Tag.find_all_next() 3133 0
Beautiful Soup with NavigableString ,comments and other special string 1129 0
Python configuring logging 303 0
Python logging.getLogRecordFactory() 1493 0
Python logging.config.fileConfig() 3391 0
Python BeautifulSoup : Scrape the Review of mask from Amazon 876 0
python logging.getLogger().getEffectivelevel() 371 1
Beautiful Soup Searching Parse Tree with find_all() 662 0
Python Beautiful Soup introduction 371 0
Python logging.basicConfig() 4028 0
Python Beautiful Soup : Scrape the Redmi mobile detail from Amazon 621 0
python logging.getLogger() 688 0
Python logging.FileHandler() 4169 0
Python logging.getLogger().isEnabledFor(level) 1683 0
Python BeautifulSoup.Tag.find_previous_siblings() and BeautifulSoup.Tag.find_previous_sibling() 1453 0
Python BeautifulSoup.find() 448 0
logging logging.StreamHandler() 3558 0
Python BeautifulSoup.Tag.find_previous() and Beautiful.Tag.find_all_previous() 2583 0
Python logging.getLogger().hasHandlers() 1714 0
Python handlers in logging 294 0
Python logging.exception() 544 0
Python Beautiful Soup : Scrape the Rate of Movie from IMDb 470 0
Python BeautifulSoup.Tag.find_parent() and BeautifulSoup.Tag.find_parents() 4257 0
Python logging.Handler.addFilter() 3391 0
Python logging.log() 328 0
Beautiful Soup with tag object 335 0
Python logging.config.dictConfig() 1723 0
Python BeautifulSoup: Scape Internship from Internshala 1086 0
python logging.getLogger().setLevel() 3962 0
Python logging introduction 449 0

Comments