1
0
Fork 0
onnx-web/api/onnx_web/logging.py

72 lines
2.6 KiB
Python
Raw Normal View History

2023-03-16 23:34:47 +00:00
import logging
2023-01-28 23:09:19 +00:00
from logging.config import dictConfig
from os import environ, path
2023-02-05 13:53:26 +00:00
2023-01-28 23:09:19 +00:00
from yaml import safe_load
2023-02-05 13:53:26 +00:00
logging_path = environ.get("ONNX_WEB_LOGGING_PATH", "./logging.yaml")
2023-01-28 23:09:19 +00:00
2023-03-16 23:34:47 +00:00
2023-03-16 23:47:04 +00:00
def add_logging_level(level_name, level_num, method_name=None):
2023-03-16 23:34:47 +00:00
"""
From https://stackoverflow.com/a/35804945
Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.
`levelName` becomes an attribute of the `logging` module with the value
`levelNum`. `methodName` becomes a convenience method for both `logging`
itself and the class returned by `logging.getLoggerClass()` (usually just
`logging.Logger`). If `methodName` is not specified, `levelName.lower()` is
used.
To avoid accidental clobberings of existing attributes, this method will
raise an `AttributeError` if the level name is already an attribute of the
`logging` module or if the method name is already present
Example
-------
>>> addLoggingLevel('TRACE', logging.DEBUG - 5)
>>> logging.getLogger(__name__).setLevel("TRACE")
>>> logging.getLogger(__name__).trace('that worked')
>>> logging.trace('so did this')
>>> logging.TRACE
5
"""
2023-03-16 23:47:04 +00:00
if not method_name:
method_name = level_name.lower()
2023-03-16 23:34:47 +00:00
2023-03-16 23:47:04 +00:00
if hasattr(logging, level_name):
raise AttributeError("{} already defined in logging module".format(level_name))
if hasattr(logging, method_name):
raise AttributeError("{} already defined in logging module".format(method_name))
if hasattr(logging.getLoggerClass(), method_name):
raise AttributeError("{} already defined in logger class".format(method_name))
2023-03-16 23:34:47 +00:00
# This method was inspired by the answers to Stack Overflow post
# http://stackoverflow.com/q/2183233/2988730, especially
# http://stackoverflow.com/a/13638084/2988730
2023-03-17 01:22:20 +00:00
def log_for_level(self, message, *args, **kwargs):
2023-03-17 02:45:57 +00:00
self._log(level_num, message, args, **kwargs)
2023-03-16 23:35:30 +00:00
2023-03-17 01:22:20 +00:00
def log_to_root(message, *args, **kwargs):
2023-03-16 23:47:04 +00:00
logging.log(level_num, message, *args, **kwargs)
2023-03-16 23:34:47 +00:00
2023-03-16 23:47:04 +00:00
logging.addLevelName(level_num, level_name)
setattr(logging, level_name, level_num)
2023-03-17 01:22:20 +00:00
setattr(logging.getLoggerClass(), method_name, log_for_level)
setattr(logging, method_name, log_to_root)
2023-03-16 23:34:47 +00:00
2023-01-28 23:09:19 +00:00
# setup logging config before anything else loads
2023-03-17 01:22:20 +00:00
add_logging_level("TRACE", logging.DEBUG - 5)
2023-01-28 23:09:19 +00:00
try:
2023-02-05 13:53:26 +00:00
if path.exists(logging_path):
with open(logging_path, "r") as f:
config_logging = safe_load(f)
dictConfig(config_logging)
2023-01-28 23:09:19 +00:00
except Exception as err:
2023-02-05 13:53:26 +00:00
print("error loading logging config: %s" % (err))