all Python 3loggingfunction
logging.captureWarnings()
configuration
Redirects warnings from the warnings module into logging: they are written by the 'py.warnings' logger at level WARNING.
Syntax
logging.captureWarnings(capture)
Until the 'py.warnings' logger or an ancestor has a handler, captured warnings are not shown at all: the old stderr output is already switched off. Capturing does not bypass warnings filters — by default the same warning from the same line reaches the log only once. The record text is the ready-made formatwarning() string with the file path and line number.
Examples
import logging, sys, warnings
logging.basicConfig(stream=sys.stdout, format='%(name)s %(levelname)s')
logging.captureWarnings(True)
warnings.warn('устарело') # → py.warnings WARNING
logging.captureWarnings(False)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy