Sub topic 5
Handlers: where records go
This chapter assumes you know how to create a named logger with getLogger and how the five log levels work. A handler is the object that sends a log record to a destination such as the console, a file, or a network service.
What a handler does
A logger creates a record and decides whether its level passes. It does not write anything itself. It hands the record to each handler attached to it, and each handler delivers the record to one destination.
You can attach several handlers to one logger. Each handler has its own level and its own formatter, so one record can go to different places in different forms.
Common handler classes
| Handler | Destination |
|---|---|
| `StreamHandler` | A stream, `sys.stderr` by default |
| `FileHandler` | A file on disk |
| `NullHandler` | Nowhere. It discards records |
| `RotatingFileHandler` | A file that rolls over at a size limit |
| `QueueHandler` | A queue that another thread drains |
StreamHandler and FileHandler live in logging. The others live in logging.handlers, except NullHandler, which is in logging.
Two handlers on one logger
import logging
logger = logging.getLogger("myapp")
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
file_handler = logging.FileHandler("app.log", encoding="utf-8")
file_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(name)s: %(message)s")
console.setFormatter(formatter)
file_handler.setFormatter(formatter)
logger.addHandler(console)
logger.addHandler(file_handler)
logger.debug("Written to the file only")
logger.info("Written to the file and the console")Follow the two calls at the bottom:
logger.debug(...)passes the logger's DEBUG level. The console handler rejects it because its level is INFO. The file handler accepts it.logger.info(...)passes the logger's level and both handlers' levels, so it appears in both places.
The logger's level is the first gate. A record that fails it never reaches any handler. That is why the logger is set to DEBUG here: if it were set to INFO, the file handler would never see a DEBUG record, whatever its own level said.
Common errors
- Handler level set, logger level forgotten. A new logger has no level of its own and inherits one from its ancestors, which usually means WARNING. Setting a handler to DEBUG then has no effect, because the logger drops the records first.
- Duplicate handlers. Every
addHandlercall adds another handler. If setup code runs twice, for example inside a function called per request, each message is written twice. Configure handlers once at startup. - No formatter. A handler without a formatter writes only the message text, with no time or level.
- Missing encoding.
FileHandleruses the platform's default encoding unless you passencoding="utf-8". Non-ASCII text can then fail or differ between machines. - Repeated lines through the root logger. If the root logger also has a handler, for example from
basicConfig, records reach both handlers because of propagation. The next chapters explain how propagation works.
Exercise
A logger is set to INFO, and its file handler is set to DEBUG. Does a call to logger.debug("x") appear in the file? Why or why not?
Show answer
No. The logger checks its own level first, and DEBUG is below INFO, so the record is dropped before any handler sees it. To write DEBUG records to the file, set the logger's level to DEBUG as well.