Python Glossary Русский

all Python 3loggingterm

logging.Handler

handlers

The base class of every log handler. It decides where the records go and how: to the console, a file, the network. Not used directly.

Syntax

handler.setLevel(level)
handler.setFormatter(formatter)
logger.addHandler(handler)

The level is checked twice, first by the logger and then by the handler, so setLevel(DEBUG) on a handler changes nothing while the logger itself sits at WARNING. One logger can carry several handlers with different levels and formatters — the usual pairing is full detail to a file, errors only to the console. To write your own, subclass Handler and implement emit(); calling addHandler inside a function that runs more than once is the classic source of duplicated lines.

Examples

import logging
# Handler — абстрактный базовый класс
# Используй конкретные подклассы:
sh = logging.StreamHandler()   # консоль
fh = logging.FileHandler('app.log')  # файл

See also

Open in the full glossarydocs.python.orgReport an inaccuracy