Skip to content
Snippets Groups Projects
Commit 9188839c authored by Martin Lange's avatar Martin Lange
Browse files

add two more log levels and resp. tests

parent afd98511
No related branches found
No related tags found
1 merge request!240More log levels
Pipeline #134906 passed with stages
in 7 minutes and 16 seconds
......@@ -246,7 +246,7 @@ There you have several options:
- ``<pathlike>``: log file will be created under the given path
- ``log_level``: (int) this will control the level of logging (:data:`logging.INFO` by default)
- only log messages with a level equal or higher than the given logging level will be shown
- options are (from most to least verbose): :data:`logging.DEBUG`, :data:`logging.INFO`, :data:`logging.WARNING`, :data:`logging.ERROR`, :data:`logging.CRITICAL` or any positive integer number
- options are (from most to least verbose): :data:`logging.TRACE`, :data:`logging.DEBUG`, :data:`logging.PROFILE`, :data:`logging.INFO`, :data:`logging.WARNING`, :data:`logging.ERROR`, :data:`logging.CRITICAL` or any positive integer number
A log file could look like this, when setting the logging level to :data:`logging.INFO`:
......
......@@ -187,6 +187,9 @@ except ModuleNotFoundError: # pragma: no cover
# package is not installed
__version__ = "0.0.0.dev0"
tools.log_helper.add_logging_level("TRACE", 5)
tools.log_helper.add_logging_level("PROFILE", 15)
__all__ = ["__version__"]
__all__ += ["adapters", "data", "interfaces", "modules", "schedule", "sdk", "tools"]
......
......@@ -12,6 +12,7 @@ Logging helper
LogStdOutStdErr
LogWriter
is_loggable
add_logging_level
Datetime helper
===============
......@@ -67,6 +68,7 @@ from .log_helper import (
LogCStdOutStdErr,
LogStdOutStdErr,
LogWriter,
add_logging_level,
is_loggable,
)
......@@ -75,6 +77,7 @@ __all__ += ["is_timedelta"]
__all__ += ["get_enum_value"]
__all__ += ["inspect"]
__all__ += [
"add_logging_level",
"ErrorLogger",
"is_loggable",
"LogWriter",
......
......@@ -146,3 +146,48 @@ class ErrorLogger(AbstractContextManager):
def __exit__(self, exc_type, exc_value, traceback):
if exc_value is not None and self.do_log:
logging.getLogger(self.logger).exception(exc_value)
def add_logging_level(name, num, method=None):
"""
Adds a logging level to the `logging` module.
Examples
--------
.. code-block:: Python
add_logging_level("TRACE", logging.DEBUG - 5)
Parameters
----------
name : str
The name of the new logging level.
num : int
The numeric severity of the new logging level.
method : str, optional
The method name for the new logging level.
Defaults to lowercase of ``name``.
"""
if not method:
method = name.lower()
if hasattr(logging, name):
raise AttributeError(f"{name} already defined in logging module")
if hasattr(logging, name):
raise AttributeError(f"{name} already defined in logging module")
if hasattr(logging.getLoggerClass(), name):
raise AttributeError(f"{name} already defined in logger class")
def log_for_level(self, message, *args, **kwargs):
if self.isEnabledFor(num):
# pylint: disable=protected-access
self._log(num, message, args, **kwargs)
def log_to_root(message, *args, **kwargs):
logging.log(num, message, *args, **kwargs)
logging.addLevelName(num, name)
setattr(logging, name, num)
setattr(logging.getLoggerClass(), method, log_for_level)
setattr(logging, method, log_to_root)
......@@ -29,6 +29,17 @@ class TestLog(unittest.TestCase):
# check that we only got the one dummy log
self.assertEqual(len(captured.records), 1)
def test_log_levels(self):
with self.assertLogs("foo", level="TRACE") as captured:
logging.getLogger("foo").trace("A")
logging.getLogger("foo").profile("B")
self.assertEqual(len(captured.records), 2)
self.assertEqual(captured.records[0].levelno, logging.TRACE)
self.assertEqual(captured.records[0].message, "A")
self.assertEqual(captured.records[1].levelno, logging.PROFILE)
self.assertEqual(captured.records[1].message, "B")
def test_redirect(self):
with self.assertLogs() as captured:
with LogStdOutStdErr():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment