Python logging to multiple handlers, at different log levels?

前端 未结 2 1240
孤独总比滥情好
孤独总比滥情好 2020-12-30 00:42

Folks,

I\'m scratching my head on a python logging config that I can\'t get right.

Let\'s say I have the following package installed:

mypacka         


        
2条回答
  •  -上瘾入骨i
    2020-12-30 01:01

    An approach to update your handler:

    import logging
    
    from rootmodule.mymodule import mylogger
    
    def update_handler_level(logger,  handler_type, level="INFO"):
        # if not root logger user logger.parent
        for handler in logger.handlers or logger.parent.handlers:
            if isinstance(handler, handler_type):
                print(handler.level)
                handler.setLevel(getattr(logging, level, "INFO"))
                print(handler.level)
    
    mylogger.debug('test')
    update_handler_level(mylogger, logging.StreamHandler)
    mylogger.debug('test')
    

    My logging.cfg is pretty similar than your except the fact that the logger name si set in a constant module (a can do module rename without breaking the logging configuration)

    To update from command line, you must have a mapping between your opts value and logging.Handler sub class name.

提交回复
热议问题