logging remove / inspect / modify handlers configured by fileConfig()

前端 未结 5 1650
醉梦人生
醉梦人生 2021-02-03 17:10

How can I remove / inspect / modify handlers configured for my loggers using the fileConfig() function?

For removing there is Logger.removeHandler(hdlr) method, but how

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-03 17:15

    The answer by @Mickey-Perlstein was just what I was looking for, but the listing appears to come from a much more complete version than the code provided.
    This code is kind of even cruder, but crucially for my use and understanding, includes the root logger

    import logging
    
    def listloggers():
        rootlogger = logging.getLogger()
        print(rootlogger)
        for h in rootlogger.handlers:
            print('     %s' % h)
    
        for nm, lgr in logging.Logger.manager.loggerDict.items():
            print('+ [%-20s] %s ' % (nm, lgr))
            if not isinstance(lgr, logging.PlaceHolder):
                for h in lgr.handlers:
                    print('     %s' % h)
    

    output:

    
         
          (DEBUG)>
    + [concurrent.futures  ]  
    + [concurrent          ]  
    + [asyncio             ]  
    + [myapp               ]  
    + [flask.app           ]  
    + [flask               ]  
    + [werkzeug            ]  
    

提交回复
热议问题