logging with filters

前端 未结 4 1841
难免孤独
难免孤独 2020-12-01 05:31

I\'m using Logging (import logging) to log messages.

Within 1 single module, I am logging messages at the debug level my_logger.debug(\'msg\')

4条回答
  •  甜味超标
    2020-12-01 05:58

    Do not use global. It's an accident waiting to happen.

    You can give your loggers any "."-separated names that are meaningful to you.

    You can control them as a hierarchy. If you have loggers named a.b.c and a.b.d, you can check the logging level for a.b and alter both loggers.

    You can have any number of loggers -- they're inexpensive.

    The most common design pattern is one logger per module. See Naming Python loggers

    Do this.

    import logging
    
    logger= logging.getLogger( "module_name" )
    logger_a = logger.getLogger( "module_name.function_a" )
    logger_b = logger.getLogger( "module_name.function_b" )
    
    def function_a( ... ):
        logger_a.debug( "a message" )
    
    def function_b( ... ):
        logger_b.debug( "another message" )
    
    if __name__ == "__main__":
        logging.basicConfig( stream=sys.stderr, level=logging.DEBUG )
        logger_a.setLevel( logging.DEBUG )
        logger_b.setLevel( logging.WARN )
    
        ... etc ...
    

提交回复
热议问题