Passed
Push — master ( 6d8539...583fdd )
by Konstantin
06:47
created

ocrd_utils.logging.initLogging()   C

Complexity

Conditions 10

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 47
rs 5.9999
c 0
b 0
f 0
cc 10
nop 3

How to fix   Complexity   

Complexity

Complex classes like ocrd_utils.logging.initLogging() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
Logging setup
3
4
By default: Log with lastResort logger, usually STDERR.
5
6
Logging can be overridden either programmatically in code using the library or by creating one or more of
7
8
- /etc/ocrd_logging.py
9
- $HOME/ocrd_logging.py
10
- $PWD/ocrd_logging.py
11
12
These files will be executed in the context of ocrd/ocrd_logging.py, with `logging` global set.
13
14
Changes as of 2023-08-20:
15
16
    - Try to be less intrusive with OCR-D specific logging conventions to
17
      make it easier and less surprising to define logging behavior when
18
      using OCR-D/core as a library
19
    - Change setOverrideLogLevel to only override the log level of the ``ocrd``
20
      logger and its descendants
21
    - initLogging will set exactly one handler, for the root logger or for the
22
      ``ocrd`` logger.
23
    - Child loggers should propagate to the ancestor logging (default
24
      behavior of the logging library - no more PropagationShyLogger)
25
    - disableLogging only removes any handlers from the ``ocrd`` logger
26
"""
27
# pylint: disable=no-member
28
29
from __future__ import absolute_import
30
31
from traceback import format_stack
32
33
import logging
34
import logging.config
35
from pathlib import Path
36
import sys
37
38
from .constants import LOG_FORMAT, LOG_TIMEFMT
39
from .config import config
40
41
42
__all__ = [
43
    'disableLogging',
44
    'getLevelName',
45
    'getLogger',
46
    'initLogging',
47
    'logging',
48
    'setOverrideLogLevel',
49
]
50
51
LOGGING_DEFAULTS = {
52
    '': logging.WARNING,
53
    'ocrd': logging.INFO,
54
    'ocrd_network': logging.INFO,
55
    # 'ocrd.resolver': logging.INFO,
56
    # 'ocrd.resolver.download_to_directory': logging.INFO,
57
    # 'ocrd.resolver.add_files_to_mets': logging.INFO,
58
    # To cut back on the `Self-intersection at or near point` INFO messages
59
    'shapely.geos': logging.ERROR,
60
    'tensorflow': logging.ERROR,
61
    'PIL': logging.INFO,
62
    'paramiko.transport': logging.INFO,
63
    'uvicorn.access': logging.DEBUG,
64
    'uvicorn.error': logging.DEBUG,
65
    'uvicorn': logging.INFO,
66
    'multipart': logging.INFO,
67
}
68
69
_initialized_flag = False
70
71
_ocrdLevel2pythonLevel = {
72
    'TRACE': 'DEBUG',
73
    'OFF': 'CRITICAL',
74
    'FATAL': 'ERROR',
75
}
76
77
def tf_disable_interactive_logs():
78
    try:
79
        from os import environ
80
        # This env variable must be set before importing from Keras
81
        environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
82
        from tensorflow.keras.utils import disable_interactive_logging
83
        # Enabled interactive logging throws an exception
84
        # due to a call of sys.stdout.flush()
85
        disable_interactive_logging()
86
    except ImportError:
87
        # Nothing should be handled here if TF is not available
88
        pass
89
90
def getLevelName(lvl):
91
    """
92
    Get (string) python logging level for (string) spec-defined log level name.
93
    """
94
    lvl = _ocrdLevel2pythonLevel.get(lvl, lvl)
95
    return logging.getLevelName(lvl)
96
97
def getLogger(*args, **kwargs):
98
    """
99
    Wrapper around ``logging.getLogger`` that calls :py:func:`initLogging` if
100
    that wasn't explicitly called before.
101
    """
102
    logger = logging.getLogger(*args, **kwargs)
103
    return logger
104
105
def setOverrideLogLevel(lvl, silent=not config.OCRD_LOGGING_DEBUG):
106
    """
107
    Override the output log level of the handlers attached to the ``ocrd`` logger.
108
109
    Args:
110
        lvl (string): Log level name.
111
        silent (boolean): Whether to log the override call
112
    """
113
    if lvl is not None:
114
        lvl = getLevelName(lvl)
115
        if not _initialized_flag:
116
            initLogging(silent=silent)
117
        # affect all configured loggers
118
        for logger_name in logging.root.manager.loggerDict:
119
            if not silent:
120
                print(f'[LOGGING] Overriding {logger_name} log level to {lvl}', file=sys.stderr)
121
            logging.getLogger(logger_name).setLevel(lvl)
122
123
def get_logging_config_files():
124
    """
125
    Return a list of all ``ocrd_logging.conf`` files found in CWD, HOME or /etc.
126
    """
127
    CONFIG_PATHS = [
128
        Path.cwd(),
129
        Path.home(),
130
        Path('/etc'),
131
    ]
132
    return [f for f \
133
            in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \
134
            if f.exists()]
135
136
def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG):
137
    """
138
    Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig
139
140
    initLogging is to be called by OCR-D/core once, i.e.
141
        -  for the ``ocrd`` CLI
142
        -  for the processor wrapper methods
143
144
    Other processes that use OCR-D/core as a library can, but do not have to, use this functionality.
145
146
    Keyword Args:
147
        - builtin_only (bool, False): Whether to search for logging configuration
148
                                      on-disk (``False``) or only use the
149
                                      hard-coded config (``True``). For testing
150
        - force_reinit (bool, False): Whether to ignore the module-level
151
                                      ``_initialized_flag``. For testing only.
152
        - silent (bool, True): Whether to log logging behavior by printing to stderr
153
    """
154
    global _initialized_flag
155
    if _initialized_flag:
156
        if force_reinit:
157
            disableLogging(silent=silent)
158
        else:
159
            return
160
161
    config_file = None
162
    if not builtin_only:
163
        config_file = get_logging_config_files()
164
    if config_file:
165
        if len(config_file) > 1 and not silent:
166
            print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr)
167
        config_file = config_file[0]
168
        if not silent:
169
            print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr)
170
        logging.config.fileConfig(config_file)
171
    else:
172
        if not silent:
173
            print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr)
174
        # Default logging config
175
        ocrd_handler = logging.StreamHandler(stream=sys.stderr)
176
        ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT))
177
        ocrd_handler.setLevel(logging.DEBUG)
178
        root_logger = logging.getLogger('')
179
        root_logger.addHandler(ocrd_handler)
180
        for logger_name, logger_level in LOGGING_DEFAULTS.items():
181
            logging.getLogger(logger_name).setLevel(logger_level)
182
    _initialized_flag = True
183
184
def disableLogging(silent=not config.OCRD_LOGGING_DEBUG):
185
    """
186
    Disables all logging of the ``ocrd`` logger and descendants
187
188
    Keyword Args:
189
        - silent (bool, True): Whether to log logging behavior by printing to stderr
190
    """
191
    global _initialized_flag # pylint: disable=global-statement
192
    if _initialized_flag and not silent:
193
        print("[LOGGING] Disabling logging", file=sys.stderr)
194
    _initialized_flag = False
195
    # remove all handlers we might have added (via initLogging on builtin or file config)
196
    for logger_name in logging.root.manager.loggerDict:
197
        if not silent:
198
            print(f'[LOGGING] Resetting {logger_name} log level and handlers')
199
        logger = logging.getLogger(logger_name)
200
        logger.setLevel(logging.NOTSET)
201
        for handler in logger.handlers[:]:
202
            logger.removeHandler(handler)
203
    for handler in logging.root.handlers[:]:
204
        logging.root.removeHandler(handler)
205
    # Python default log level is WARNING
206
    logging.root.setLevel(logging.WARNING)
207
208