Coverage for icet/input_output/logging_tools.py: 83%
28 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-26 04:12 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-26 04:12 +0000
1import logging
2import sys
4# This is the root logger of icet
5logger = logging.getLogger('icet')
7# Set the format which is simpler for the console
8FORMAT = '%(name)s: %(levelname)s %(message)s'
9formatter = logging.Formatter(FORMAT)
11# Will process all levels of DEBUG or higher
12logger.setLevel(logging.DEBUG)
14# If you know what you are doing you may set this to True
15logger.propagate = False
17# The logger will collect events from children and the default
18# behaviour is to print it directly to stdout
19sh = logging.StreamHandler(sys.stdout)
20sh.setFormatter(formatter)
21sh.setLevel(logging.WARNING)
22logger.addHandler(sh)
25class MyFilter(logging.Filter):
26 def __init__(self, level):
27 self.__level = level
29 def filter(self, logRecord):
30 return logRecord.levelno == self.__level
33def set_log_config(filename=None, level=None, restricted=False):
35 # If a filename is provided a logfile will be created
36 if filename is not None:
37 fh = logging.FileHandler(filename)
38 fh.setFormatter(formatter)
39 logger.addHandler(fh)
41 # Restrict file handler to only write out at the level specified
42 if restricted: 42 ↛ 43line 42 didn't jump to line 43, because the condition on line 42 was never true
43 lvl = logging.getLevelName(level)
44 fh.addFilter(MyFilter(lvl))
46 if level is not None:
47 fh.setLevel(level)
49 # If only level is provided, the stream handler will be reset to this level
50 elif level is not None: 50 ↛ exitline 50 didn't return from function 'set_log_config', because the condition on line 50 was never false
51 sh.setLevel(level)