Coverage for yasfpy/log.py: 88%
33 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-15 20:36 +0100
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-15 20:36 +0100
1import logging
3NUMERICS = 15
4SCATTER = 25
7def _log_for_numerics(self, message, *args, **kwargs):
8 if self.isEnabledFor(NUMERICS):
9 self._log(NUMERICS, message, args, **kwargs)
12def _log_for_scatter(self, message, *args, **kwargs):
13 if self.isEnabledFor(SCATTER):
14 self._log(SCATTER, message, args, **kwargs)
17# def _log_to_root(message, *args, **kwargs):
18# logging.log(level_value, message, *args, **kwargs)
21# Sources:
22# https://stackoverflow.com/questions/7621897/python-logging-module-globally
23# https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility
24def scattering_logger(name):
25 levels = ["NUMERICS", "SCATTER"]
26 methods = [_log_for_numerics, _log_for_scatter]
27 for i, level in enumerate(levels):
28 level_name = level
29 level_value = globals()[level]
30 method_name = level_name.lower()
31 if hasattr(logging, level_name):
32 # print('{} already defined in logging module'.format(level_name))
33 continue
34 if hasattr(logging, method_name):
35 # print('{} already defined in logging module'.format(method_name))
36 continue
37 if hasattr(logging.getLoggerClass(), method_name):
38 # print('{} already defined in logger class'.format(method_name))
39 continue
41 logging.addLevelName(level_value, level_name)
42 setattr(logging, level_name, level_value)
43 setattr(logging.getLoggerClass(), method_name, methods[i])
44 # setattr(logging, method_name, _log_to_root)
46 logger = logging.getLogger(name)
47 if not logger.hasHandlers():
48 formatter = logging.Formatter(fmt="%(levelname)s (%(name)s): %(message)s")
49 handler = logging.StreamHandler()
50 handler.setFormatter(formatter)
51 logger.addHandler(handler)
52 # logger.handlers.clear()
53 logger.propagate = False
55 return logger