logging – event logging
This module provides a lightweight subset of the standard Python
logging package adapted for MicroPython. It supports leveled logging
through hierarchically-named Logger objects, a small set of handlers
(StreamHandler, FileHandler), printf-style formatting via Formatter,
and module-level convenience functions equivalent to those operating on the
root logger.
The module follows the familiar CPython API closely enough that simple applications written for the standard library should work unchanged. Filters, multiple-process locking, the configuration file format, and most handler classes are not provided.
Level Constants
Functions
- logging.getLogger(name: str | None = None) Logger
Return the
Loggerregistered under name, creating it on first use. If name isNonethe root logger is returned. The first call withname="root"(or with name unset) implicitly invokesbasicConfigto attach a defaultStreamHandlerwriting tosys.stderr.
- logging.log(level: int, msg: str, *args) None
Log a message at level on the root logger. args are interpolated into msg using printf-style
%formatting; a single dict argument is used as the mapping.
- logging.exception(msg: str, *args, exc_info: bool | BaseException = True) None
Equivalent to
getLogger().exception(msg, *args, exc_info=exc_info). Logs the message atERRORand additionally formats the active exception’s traceback when exc_info is truthy. If exc_info is itself aBaseException, that exception’s traceback is used; otherwisesys.exc_info()is consulted when available.
- logging.shutdown() None
Close every handler attached to every known logger and forget the loggers. Registered automatically through
sys.atexitwhen that hook is available.
- logging.addLevelName(level: int, name: str) None
Associate the textual name with the numeric level so that
Formattercan render it via%(levelname)s.
- logging.basicConfig(filename: str | None = None, filemode: str = 'a', format: str | None = None, datefmt: str | None = None, level: int = WARNING, stream=None, encoding: str = 'UTF-8', force: bool = False) None
Configure the root logger with a single handler.
If filename is supplied a
FileHandleris created using filemode and encoding; otherwise aStreamHandlerwriting to stream (defaulting tosys.stderr) is used.format and datefmt are passed through to a new
Formatter.level sets both the handler’s level and the root logger’s level.
If the root logger already has handlers this function is a no-op unless force is
True, in which case the existing handlers are closed and replaced.
Classes
- class logging.Logger(name: str, level: int = NOTSET)
A named logger. Construct loggers via
getLoggerrather than directly, so thatgetLoggercan return the same instance for the same name.- handlers: list
The list of
Handlerinstances attached to this logger. When empty, messages are dispatched to the root logger’s handlers.
- getEffectiveLevel() int
Return the first non-zero of: this logger’s level, the root logger’s level, or
WARNING.
- log(level: int, msg: str, *args) None
Log msg at level. args are interpolated into msg with the
%operator; if the first positional argument is a dict it is used as a mapping.
- class logging.Handler(level: int = NOTSET)
Base class for all handlers. Sub-classes implement
emit().- close() None
Release resources held by the handler. Called by
shutdownand byFileHandlerwhen it is closed.
- class logging.StreamHandler(stream=None)
Handler that writes formatted records, followed by
self.terminator("\n"by default), to stream. stream defaults tosys.stderr.- stream
The destination stream object.
- class logging.FileHandler(filename: str, mode: str = 'a', encoding: str = 'UTF-8')
StreamHandlersubclass that opens filename with the given mode and encoding and writes formatted records to it. The underlying file is closed onclose().
- class logging.Formatter(fmt: str | None = None, datefmt: str | None = None)
Renders
LogRecordinstances to strings.fmt is a printf-style template. Recognised keys are
%(name)s,%(message)s,%(msecs)d,%(asctime)s, and%(levelname)s. When unset it defaults to"%(levelname)s:%(name)s:%(message)s".datefmt is the
time.strftimetemplate used to render%(asctime)s. Defaults to"%Y-%m-%d %H:%M:%S".- formatTime(datefmt: str, record: LogRecord) str | None
Format
record.ctusingtime.strftimeand datefmt. ReturnsNoneon platforms wheretime.strftimeis not available.
- format(record: LogRecord) str
Render record. If the template uses
asctime,formatTime()is invoked first to populaterecord.asctime.