module documentation

Common structured logging utilities.

Class LogFormat Structured logging format to use.
Function console_formatter Formatter rendering structlog and plain stdlib records as Key=Value lines, like LogFormat.KEY_VALUE.
Function json_formatter Formatter rendering structlog and plain stdlib records as JSON lines, like LogFormat.JSON.
Function setup_logging Set up structured logging.
Constant DEFAULT_LOG_LEVELS Undocumented
Function _console_formatter Undocumented
Function _elapsed_to_ms Convert elapsed entry in structlog processor event dict to shorter format.
Function _exception_line Render an exception as its last traceback line ExcType: message only, after the log line.
Function _file_handler Undocumented
Function _flag Undocumented
Function _format_elapsed Format given seconds into short human-readable string.
Function _from_env Return the environment variable name converted; value if it's unset or invalid.
Function _level Undocumented
Function _path_to_str Convert path entry in structlog processor event dict to simple string.
Function _resolve_format AUTOMATIC: KEY_VALUE on a terminal, else JSON.
Function _set_levels Undocumented
Function _shared_processors Processors for structlog and foreign (plain stdlib) records alike, before rendering.
Constant _LOGGER Undocumented
Variable _logging_initialized Global flag indicated whether logging is initialized.
def console_formatter(*, colors: bool = True, tracebacks: bool = True) -> logging.Formatter:

Formatter rendering structlog and plain stdlib records as Key=Value lines, like LogFormat.KEY_VALUE.

For handlers of applications with their own logging setup, after setup_logging with LogFormat.KEY_VALUE.

Parameters
colors:boolANSI colors in the output; also the local variables in Rich tracebacks.
tracebacks:boolFull tracebacks (Rich if installed); otherwise only their last line ExcType: message.
Returns
logging.FormatterUndocumented
def json_formatter() -> logging.Formatter:

Formatter rendering structlog and plain stdlib records as JSON lines, like LogFormat.JSON.

For handlers of applications with their own logging setup, after setup_logging with LogFormat.JSON.

def setup_logging(*, log_min_level: int | str = logging.NOTSET, log_levels: Mapping[str, int | str] | None = None, log_format: LogFormat = LogFormat.AUTOMATIC, enable_rich_tracebacks: bool = True, force_basic_config: bool = False, console: bool = True, log_file: str | os.PathLike[str] | None = None, log_file_level: int | str = logging.NOTSET, log_file_max_bytes: int = 0, log_file_backups: int = 0):

Set up structured logging.

Environment variables override the arguments: LOG_FORMAT (key_value, json, automatic), LOG_LEVEL (log_min_level), LOG_CONSOLE (1 / 0), LOG_FILE (empty: none), LOG_FILE_LEVEL, LOG_FILE_MAX_BYTES and LOG_FILE_BACKUPS. Invalid values are logged and ignored.

If the root logger already has handlers (and force_basic_config is false), none are added: those handlers need console_formatter to render structlog records.

Parameters
log_min_level:int | strMinimum logging level for structlog loggers.
log_levels:Mapping[str, int | str] | NoneLogging levels for all loggers. Maps logger name to level. Defaults to DEFAULT_LOG_LEVELS.
log_format:LogFormatLogging format for all loggers, on the console and in the log file.
enable_rich_tracebacks:boolEnable Rich tracebacks if KEY_VALUE log format is used.
force_basic_config:boolforce parameter of logging.basicConfig.
console:boolLog to stderr.
log_file:str | os.PathLike[str] | NoneAlso log to this file: appended, UTF-8, created on the first record, no colors.
log_file_level:int | strMinimum level of the records written to log_file.
log_file_max_bytes:intRotate log_file at this size; 0: never.
log_file_backups:intNumber of rotated files kept (.1, .2, ...).
DEFAULT_LOG_LEVELS: dict[str, int | str] =

Undocumented

Value
{'opensmi': logging.INFO, 'asyncua': logging.ERROR}
def _console_formatter(*, colors: bool, tracebacks: bool, rich_tracebacks: bool) -> logging.Formatter:

Undocumented

def _elapsed_to_ms(_, __, event_dict: MutableMapping[str, Any]) -> MutableMapping[str, Any]:

Convert elapsed entry in structlog processor event dict to shorter format.

def _exception_line(sio: TextIO, exc_info: Any):

Render an exception as its last traceback line ExcType: message only, after the log line.

def _file_handler(log_file: str | os.PathLike[str], *, level: int | str, max_bytes: int, backups: int) -> logging.Handler:

Undocumented

def _flag(raw: str) -> bool:

Undocumented

def _format_elapsed(seconds: float) -> str:

Format given seconds into short human-readable string.

def _from_env(name: str, value: Any, convert: Callable[[str], Any], invalid: list[str]) -> Any:

Return the environment variable name converted; value if it's unset or invalid.

def _level(raw: str) -> int:

Undocumented

def _path_to_str(_, __, event_dict: MutableMapping[str, Any]) -> MutableMapping[str, Any]:

Convert path entry in structlog processor event dict to simple string.

def _resolve_format(log_format: LogFormat) -> LogFormat:

AUTOMATIC: KEY_VALUE on a terminal, else JSON.

def _set_levels(log_min_level: int | str, log_levels: Mapping[str, int | str]):

Undocumented

def _shared_processors(*, utc: bool = False) -> list[Any]:

Processors for structlog and foreign (plain stdlib) records alike, before rendering.

_LOGGER =

Undocumented

Value
logging.getLogger(__name__)
_logging_initialized: bool =

Global flag indicated whether logging is initialized.