Python Logging add Filter example __python

Source: Internet
Author: User

Example One

def filter (self, record): "" "We
        custom record filtering logic.

        Built-in filtering logic (via logging. Filter) is too limiting.
        "" " If not self.filters: return
            True
        matched = False
        rname = record.name # shortcut to
        name in self.filters:< C8/>if Rname = = = Name or Rname.startswith (name+ '. '):
                matched = True return
        matched

Example Two

def _create_log_handlers (stream): "" "Create and return a default list of logging.

    Handler instances. 

    The Format WARNING messages and above to display the logging level and messages strictly below WARNING not to display it.

    Args:stream:See the configure_logging () docstring. "" # Handles logging.
    WARNING and above. Error_Handler = logging. Streamhandler (Stream) error_handler.setlevel (logging. WARNING) formatter = logging. Formatter ("% (levelname) s:% (message) S") Error_handler.setformatter (Formatter) # Create a logging.
    Filter instance that is only accepts messages # below WARNING (i.e. filters out anything WARNING or above). Non_error_filter = logging. Filter () # The filter method accepts a logging.
    LogRecord instance. Non_error_filter.filter = Lambda Record:record.levelno < logging. WARNING Non_error_handler = logging. Streamhandler (Stream) Non_error_handler.addfilter (non_error_filter) formatter = logging. Formatter("% (message) S") Non_error_handler.setformatter (formatter) return [Error_Handler, Non_error_handler] 

Example Three

def _default_handlers (Stream): "" "return a list of the ' Default logging handlers to use.

    Args:stream:See the configure_logging () docstring.
    "" # Create the filter. def should_log (record): "" "return whether a logging.
        LogRecord should be logged. "" "  # fixme:enable The logging of autoinstall messages once # AutoInstall is adjusted. Currently, AutoInstall logs # INFO messages when importing already-downloaded packages, # WH
        The ICH is too verbose. If Record.name.startswith ("Webkitpy.thirdparty.autoinstall"): Return False return True Logging_fi Lter = logging.
    Filter () Logging_filter.filter = should_log # Create the handler. Handler = logging. Streamhandler (stream) formatter = logging. Formatter ("% (name) s: [% (levelname) s]% (message) S") Handler.setformatter (Formatter) handler.addfilter (logging_filte R) return [handler] 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.