# SPDX-License-Identifier: MIT OR Apache-2.0# This file is dual licensed under the terms of the Apache License, Version# 2.0, and the MIT License. See the LICENSE file in the root of this# repository for complete details."""Extracted log level data used by both stdlib and native log level filters."""from__future__importannotationsimportloggingfrom.typingimportEventDict# Adapted from the stdlibCRITICAL=50FATAL=CRITICALERROR=40WARNING=30WARN=WARNINGINFO=20DEBUG=10NOTSET=0NAME_TO_LEVEL={"critical":CRITICAL,"exception":ERROR,"error":ERROR,"warn":WARNING,"warning":WARNING,"info":INFO,"debug":DEBUG,"notset":NOTSET,}LEVEL_TO_NAME={v:kfork,vinNAME_TO_LEVEL.items()ifknotin("warn","exception","notset")}# Keep around for backwards-compatability in case someone imported them._LEVEL_TO_NAME=LEVEL_TO_NAME_NAME_TO_LEVEL=NAME_TO_LEVEL
[docs]defadd_log_level(logger:logging.Logger,method_name:str,event_dict:EventDict)->EventDict:""" Add the log level to the event dict under the ``level`` key. Since that's just the log method name, this processor works with non-stdlib logging as well. Therefore it's importable both from `structlog.processors` as well as from `structlog.stdlib`. .. versionadded:: 15.0.0 .. versionchanged:: 20.2.0 Importable from `structlog.processors` (additionally to `structlog.stdlib`). .. versionchanged:: 24.1.0 Added mapping from "exception" to "error" """ifmethod_name=="warn":# The stdlib has an aliasmethod_name="warning"elifmethod_name=="exception":# exception("") method is the same as error("", exc_info=True)method_name="error"event_dict["level"]=method_namereturnevent_dict