Home / Python logging / Chapter 6

Chapter 6: LastResort Handler

Aug 22, 2024
6 of 11

There’s one special built-in handler that is unlike the rest. If, and only if, a logger has no handlers attached (that is, no handler on itself or any of its ancestors) then the LogRecord is sent to the LastResort Handler. This is a handler with output stderr, no formatting, and a log level of WARNING.

This is why, without any configuration, warning logs and up are printed to the console.

import logging

logger = logging.getLogger("__name__")
logger.warning("This is a warning message")