Python Glossary Русский

all Python 3loggingterm

logging

module

A program's event log: messages with severity levels, named loggers, output to the console and files.

Syntax

import logging

Use logging instead of print when messages must be filtered by severity, written to a file or switched off without editing code. The root logger lets only WARNING and above through by default, so logging.info stays silent until basicConfig(level=...) is called. basicConfig only takes effect once, and a second call changes nothing without force=True. Modules create their own logger with getLogger(__name__) and leave output setup to one place in the program.

Examples

import logging, sys
logging.info('not shown')
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
                    format='%(levelname)s:%(name)s:%(message)s')
log = logging.getLogger('app')
log.info('started')  # → INFO:app:started
log.debug('hidden')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy