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

前端 未结 5 1669
醉梦人生
醉梦人生 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:17

    This code will print all the loggers and for each logger its handlers

    for k,v in  logging.Logger.manager.loggerDict.items()  :
            print('+ [%s] {%s} ' % (str.ljust( k, 20)  , str(v.__class__)[8:-2]) ) 
            if not isinstance(v, logging.PlaceHolder):
                for h in v.handlers:
                    print('     +++',str(h.__class__)[8:-2] )
    

    This will print out the loggers and handlers in your system including their states and levels.

    This will help you debug your logging issues

    output:
    + [root                ] {logging.RootLogger} {DEBUG} 
    -------------------------
       -name=root
       -handlers=[, ]
       -filters=[]
       -propagate=True
       -level=10
       -disabled=False
       -parent=None
         +++logging.FileHandler {NOTSET}
       -stream=<_io.TextIOWrapper name='/dev/logs/myapp.log' mode='w' encoding='UTF-8'>
       -mode=w
       -filters=[]
       -encoding=None
       -baseFilename=/home/dev/logs/myapp.log
       -level=0
       -lock=
       -delay=False
       -_name=None
       -formatter=
         +++logging.StreamHandler {DEBUG}
       -lock=
       -filters=[]
       -stream=
       -level=10
       -_name=None
       -formatter=
    + [PathFinder          ] {logging.Logger} {NOTSET} 
    -------------------------
       -name=PathFinder
       -handlers=[]
       -filters=[]
       -manager=
       -propagate=True
       -level=0
       -disabled=False
       -parent=
    

提交回复
热议问题