Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

logging

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 do I get the handler in first place if it was configured from file?

like image 384
Pawel Furmaniak Avatar asked Sep 02 '10 20:09

Pawel Furmaniak


People also ask

What are handlers in logging?

The log handler is the component that effectively writes/displays a log: Display it in the console (via StreamHandler), in a file (via FileHandler), or even by sending you an email via SMTPHandler, etc. Each log handler has 2 important fields: A formatter which adds context information to a log.

What is logger logging getLogger (__ Name __)?

getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.


2 Answers

logger.handlers contains a list with all handlers of a logger.

like image 143
leoluk Avatar answered Oct 07 '22 02:10

leoluk


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=[<logging.FileHandler object at 0x7fc599585390>, <logging.StreamHandler object at 0x7fc599585550>]    -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=<unlocked _thread.RLock object owner=0 count=0 at 0x7fc5a85a4240>    -delay=False    -_name=None    -formatter=<logging.Formatter object at 0x7fc599585358>      +++logging.StreamHandler {DEBUG}    -lock=<unlocked _thread.RLock object owner=0 count=0 at 0x7fc5a85a4210>    -filters=[]    -stream=<ipykernel.iostream.OutStream object at 0x7fc5aa6abb00>    -level=10    -_name=None    -formatter=<logging.Formatter object at 0x7fc5995853c8> + [PathFinder          ] {logging.Logger} {NOTSET}  -------------------------    -name=PathFinder    -handlers=[]    -filters=[]    -manager=<logging.Manager object at 0x7fc5b09757f0>    -propagate=True    -level=0    -disabled=False    -parent=<logging.RootLogger object at 0x7fc5b09757b8> 
like image 35
Mickey Perlstein Avatar answered Oct 07 '22 03:10

Mickey Perlstein