# 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__importannotationsfromtypingimportAnyfrom.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_LEVELdefmap_method_name(method_name:str)->str:# warn is just a deprecated alias in the stdlib.ifmethod_name=="warn":return"warning"# Calling exception("") is the same as error("", exc_info=True)ifmethod_name=="exception":return"error"returnmethod_name
[docs]defadd_log_level(logger:Any,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" """event_dict["level"]=map_method_name(method_name)returnevent_dict