pytex/PyTeX/logger/add_logging_level.py

31 lines
1.1 KiB
Python
Raw Permalink Normal View History

2022-02-18 11:18:28 +01:00
# This file is adapted from the haggis library, available at
# https://github.com/madphysicist/haggis
#
# The haggis library is licensed under the
# GNU Affero General Public License v3.0 (AGPLv3)
#
# The code has been adapted since only a small code piece is needed
# to avoid introducing an additional install-dependency when
# using PyTeX
#
# You should have received a copy of the AGPLv3 license along with the
# PyTeX distribution. If not, refer to
# https://www.gnu.org/licenses/agpl-3.0.en.html
#
import logging
def add_logging_level(level_name: str, level_num: int):
def log_for_level(self, message, *args, **kwargs):
if self.isEnabledFor(level_num):
self._log(level_num, message, args, **kwargs)
def log_to_root(message, *args, **kwargs):
logging.log(level_num, message, *args, **kwargs)
logging.addLevelName(level_num, level_name.upper())
setattr(logging, level_name.upper(), level_num)
setattr(logging.getLoggerClass(), level_name.lower(), log_for_level)
setattr(logging, level_name.lower(), log_to_root)