On this page
logging.Formatter
Prev: logging.Filter | Up: logging | Next: logging.getHandlerByName
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.Formatter
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import Formatter
Signature & Overview
logging.Formatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)
Formatter instances are used to convert a LogRecord to text.
Methods & Attributes
Formatter.converter(...)— tm_sec,tm_wday,tm_yday,tm_isdst) Convert seconds since the Epoch to a time tuple expressing local time.Formatter.format(self, record)— Format the specified record as text.Formatter.formatException(self, ei)— Format and return the specified exception information as a string.Formatter.formatMessage(self, record)—Formatter.formatMessagemethod/property.Formatter.formatStack(self, stack_info)— This method is provided as an extension point for specialized formatting of stack information.Formatter.formatTime(self, record, datefmt=None)— Return the creation time of the specified LogRecord as formatted text.Formatter.usesTime(self)— Check if the format uses the creation time of the record.
Usage
import logging
fmt = logging.Formatter("%(levelname)s [%(name)s] %(message)s")
rec = logging.LogRecord("api", logging.WARNING, "", 1, "slow query", (), None)
assert fmt.format(rec) == "WARNING [api] slow query"
Gotchas
- Three formatting styles: Default is
style="%"(%(message)s); passstyle="{"for{levelname}: {message}orstyle="$"for$message. - Use
datefmt="%Y-%m-%dT%H:%M:%S"when you want ISO-like timestamps.
REPL Inspect
from logging import Formatter
help(Formatter)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/Filter Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/getHandlerByName 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