On this page
logging.Logger
Prev: logging.log | Up: logging | Next: logging.LoggerAdapter
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.Logger
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import Logger
Signature & Overview
logging.Logger(name, level=0)
Instances of the Logger class represent a single logging channel. A "logging channel" indicates an area of an application. Exactly how an "area" is defined is up to the application developer. Since an
Methods & Attributes
Logger.addFilter(self, filter)— Add the specified filter to this handler.Logger.addHandler(self, hdlr)— Add the specified handler to this logger.Logger.callHandlers(self, record)— Pass a record to all relevant handlers.Logger.critical(self, msg, *args, **kwargs)— Log 'msg % args' with severity 'CRITICAL'.Logger.debug(self, msg, *args, **kwargs)— Log 'msg % args' with severity 'DEBUG'.Logger.error(self, msg, *args, **kwargs)— Log 'msg % args' with severity 'ERROR'.Logger.exception(self, msg, *args, exc_info=True, **kwargs)— Convenience method for logging an ERROR with exception information.Logger.fatal(self, msg, *args, **kwargs)— Don't use this method, use critical() instead.Logger.filter(self, record)— Determine if a record is loggable by consulting all the filters.Logger.findCaller(self, stack_info=False, stacklevel=1)— Find the stack frame of the caller so that we can note the source file name, line number and function name.Logger.getChild(self, suffix)— Get a logger which is a descendant to this one.Logger.getChildren(self)—Logger.getChildrenmethod/property.Logger.getEffectiveLevel(self)— Get the effective level for this logger.Logger.handle(self, record)— Call the handlers for the specified record.Logger.hasHandlers(self)— See if this logger has any handlers configured.Logger.info(self, msg, *args, **kwargs)— Log 'msg % args' with severity 'INFO'.Logger.isEnabledFor(self, level)— Is this logger enabled for level 'level'?Logger.log(self, level, msg, *args, **kwargs)— Log 'msg % args' with the integer severity 'level'.Logger.makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)— A factory method which can be overridden in subclasses to create specialized LogRecords.Logger.removeFilter(self, filter)— Remove the specified filter from this handler.Logger.removeHandler(self, hdlr)— Remove the specified handler from this logger.Logger.setLevel(self, level)— Set the logging level of this logger. level must be an int or a str.Logger.warn(self, msg, *args, **kwargs)—Logger.warnmethod/property.Logger.warning(self, msg, *args, **kwargs)— Log 'msg % args' with severity 'WARNING'.
Usage
import logging
logger = logging.getLogger("myapp.worker")
logger.setLevel(logging.DEBUG)
assert isinstance(logger, logging.Logger)
assert logger.isEnabledFor(logging.DEBUG)
Gotchas
- Never instantiate
Logger()directly: Always uselogging.getLogger(__name__)so loggers sit in the global hierarchy and respect root configuration. - Use
%slazy formatting: Writelogger.debug("user=%s", uid)rather than an f-string so formatting is skipped when DEBUG is disabled.
REPL Inspect
from logging import Logger
help(Logger)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/log Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/LoggerAdapter Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib