feat(api): add trace log level
This commit is contained in:
parent
c8c5e9f42e
commit
959798eabb
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from logging.config import dictConfig
|
from logging.config import dictConfig
|
||||||
from os import environ, path
|
from os import environ, path
|
||||||
|
|
||||||
|
@ -5,11 +6,65 @@ from yaml import safe_load
|
||||||
|
|
||||||
logging_path = environ.get("ONNX_WEB_LOGGING_PATH", "./logging.yaml")
|
logging_path = environ.get("ONNX_WEB_LOGGING_PATH", "./logging.yaml")
|
||||||
|
|
||||||
|
|
||||||
|
def addLoggingLevel(levelName, levelNum, methodName=None):
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not methodName:
|
||||||
|
methodName = levelName.lower()
|
||||||
|
|
||||||
|
if hasattr(logging, levelName):
|
||||||
|
raise AttributeError('{} already defined in logging module'.format(levelName))
|
||||||
|
if hasattr(logging, methodName):
|
||||||
|
raise AttributeError('{} already defined in logging module'.format(methodName))
|
||||||
|
if hasattr(logging.getLoggerClass(), methodName):
|
||||||
|
raise AttributeError('{} already defined in logger class'.format(methodName))
|
||||||
|
|
||||||
|
# This method was inspired by the answers to Stack Overflow post
|
||||||
|
# http://stackoverflow.com/q/2183233/2988730, especially
|
||||||
|
# http://stackoverflow.com/a/13638084/2988730
|
||||||
|
def logForLevel(self, message, *args, **kwargs):
|
||||||
|
if self.isEnabledFor(levelNum):
|
||||||
|
self._log(levelNum, message, args, **kwargs)
|
||||||
|
def logToRoot(message, *args, **kwargs):
|
||||||
|
logging.log(levelNum, message, *args, **kwargs)
|
||||||
|
|
||||||
|
logging.addLevelName(levelNum, levelName)
|
||||||
|
setattr(logging, levelName, levelNum)
|
||||||
|
setattr(logging.getLoggerClass(), methodName, logForLevel)
|
||||||
|
setattr(logging, methodName, logToRoot)
|
||||||
|
|
||||||
|
|
||||||
# setup logging config before anything else loads
|
# setup logging config before anything else loads
|
||||||
try:
|
try:
|
||||||
if path.exists(logging_path):
|
if path.exists(logging_path):
|
||||||
with open(logging_path, "r") as f:
|
with open(logging_path, "r") as f:
|
||||||
config_logging = safe_load(f)
|
config_logging = safe_load(f)
|
||||||
dictConfig(config_logging)
|
dictConfig(config_logging)
|
||||||
|
addLoggingLevel('TRACE', logging.DEBUG - 5)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print("error loading logging config: %s" % (err))
|
print("error loading logging config: %s" % (err))
|
||||||
|
|
Loading…
Reference in New Issue