Python 3.12+loggingfunction
logging.getHandlerByName()
handlers
Returns the handler with the given name, or None if there is none. Handy after configuring logging with dictConfig.
Syntax
handler = logging.getHandlerByName(name)
Only handlers with a name are found: dictConfig() sets it for you, while handlers built in code need handler.name assigned. The registry keeps weak references, so a handler nothing else uses drops out of it. The function arrived in 3.12; on older versions calling it raises AttributeError.
Examples
import logging, sys
h = logging.StreamHandler(sys.stdout)
print(logging.getHandlerByName('console')) # → None
h.name = 'console'
print(logging.getHandlerByName('console') is h) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy