all Python 3loggingfunction
logging.log()
levels
Logs a message on the root logger at a level passed as a number in the first argument. Used for custom levels or a level held in a variable.
Syntax
logging.log(level, msg, *args, **kwargs)
The level must be a number: logging.log('INFO', ...) raises TypeError, so convert names with getLevelNamesMapping(). Pass message arguments separately rather than as an f-string, so nothing is built when the level is off. Like the other module-level functions, the first call without configuration runs basicConfig() itself.
Examples
import logging, sys
logging.basicConfig(stream=sys.stdout, format='%(levelname)s %(message)s')
level = logging.ERROR
logging.log(level, 'код %d', 500) # → ERROR код 500
try:
logging.log('INFO', 'строка вместо числа')
except TypeError as e:
print(e) # → level must be an integer
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy