Passed
Pull Request — master (#1117)
by Konstantin
02:39
created

ocrd_utils.logging.initLogging()   D

Complexity

Conditions 13

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 63
rs 4.2
c 0
b 0
f 0
cc 13
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
# These are the loggers we add handlers to
52
ROOT_OCRD_LOGGERS = [
53
    'ocrd',
54
    'ocrd_network'
55
]
56
57
LOGGING_DEFAULTS = {
58
    'ocrd': logging.INFO,
59
    'ocrd_network': logging.DEBUG,
60
    # 'ocrd.resolver': logging.INFO,
61
    # 'ocrd.resolver.download_to_directory': logging.INFO,
62
    # 'ocrd.resolver.add_files_to_mets': logging.INFO,
63
    # To cut back on the `Self-intersection at or near point` INFO messages
64
    'shapely.geos': logging.ERROR,
65
    'tensorflow': logging.ERROR,
66
    'PIL': logging.INFO,
67
    'paramiko.transport': logging.INFO,
68
    'uvicorn.access': logging.DEBUG,
69
    'uvicorn.error': logging.DEBUG,
70
    'uvicorn': logging.INFO
71
}
72
73
_initialized_flag = False
74
75
_ocrdLevel2pythonLevel = {
76
    'TRACE': 'DEBUG',
77
    'OFF': 'CRITICAL',
78
    'FATAL': 'ERROR',
79
}
80
81
def tf_disable_interactive_logs():
82
    try:
83
        from os import environ
84
        # This env variable must be set before importing from Keras
85
        environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
86
        from tensorflow.keras.utils import disable_interactive_logging
87
        # Enabled interactive logging throws an exception
88
        # due to a call of sys.stdout.flush()
89
        disable_interactive_logging()
90
    except ImportError:
91
        # Nothing should be handled here if TF is not available
92
        pass
93
94
def getLevelName(lvl):
95
    """
96
    Get (string) python logging level for (string) spec-defined log level name.
97
    """
98
    lvl = _ocrdLevel2pythonLevel.get(lvl, lvl)
99
    return logging.getLevelName(lvl)
100
101
def getLogger(*args, **kwargs):
102
    """
103
    Wrapper around ``logging.getLogger`` that alls :py:func:`initLogging` if
104
    that wasn't explicitly called before.
105
    """
106
    logger = logging.getLogger(*args, **kwargs)
107
    return logger
108
109
def setOverrideLogLevel(lvl, silent=not config.OCRD_LOGGING_DEBUG):
110
    """
111
    Override the output log level of the handlers attached to the ``ocrd`` logger.
112
113
    Args:
114
        lvl (string): Log level name.
115
        silent (boolean): Whether to log the override call
116
    """
117
    if not _initialized_flag:
118
        initLogging(silent=silent)
119
    ocrd_logger = logging.getLogger('ocrd')
120
121
    if lvl is None:
122
        if not silent:
123
            print('[LOGGING] Reset log level override', file=sys.stderr)
124
        ocrd_logger.setLevel(logging.NOTSET)
125
    else:
126
        if not silent:
127
            print(f'[LOGGING] Overriding ocrd log level to {lvl}', file=sys.stderr)
128
        ocrd_logger.setLevel(lvl)
129
130
def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG):
131
    """
132
    Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig
133
134
    initLogging is to be called by OCR-D/core once, i.e.
135
        -  for the ``ocrd`` CLI
136
        -  for the processor wrapper methods
137
138
    Other processes that use OCR-D/core as a library can, but do not have to, use this functionality.
139
140
    Keyword Args:
141
        - builtin_only (bool, False): Whether to search for logging configuration
142
                                      on-disk (``False``) or only use the
143
                                      hard-coded config (``True``). For testing
144
        - force_reinit (bool, False): Whether to ignore the module-level
145
                                      ``_initialized_flag``. For testing only.
146
        - silent (bool, True): Whether to log logging behavior by printing to stderr
147
    """
148
    global _initialized_flag
149
    if _initialized_flag and not force_reinit:
150
        return
151
    # disableLogging()
152
153
    # https://docs.python.org/3/library/logging.html#logging.disable
154
    # If logging.disable(logging.NOTSET) is called, it effectively removes this
155
    # overriding level, so that logging output again depends on the effective
156
    # levels of individual loggers.
157
    logging.disable(logging.NOTSET)
158
159
    # remove all handlers for the ocrd root loggers
160
    for logger_name in ROOT_OCRD_LOGGERS:
161
        for handler in logging.getLogger(logger_name).handlers[:]:
162
            logging.getLogger(logger_name).removeHandler(handler)
163
164
    config_file = None
165
    if not builtin_only:
166
        CONFIG_PATHS = [
167
            Path.cwd(),
168
            Path.home(),
169
            Path('/etc'),
170
        ]
171
        config_file = [f for f \
172
                in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \
173
                if f.exists()]
174
    if config_file:
175
        if len(config_file) > 1 and not silent:
176
            print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr)
177
        config_file = config_file[0]
178
        if not silent:
179
            print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr)
180
        logging.config.fileConfig(config_file)
181
    else:
182
        if not silent:
183
            print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr)
184
        # Default logging config
185
        ocrd_handler = logging.StreamHandler(stream=sys.stderr)
186
        ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT))
187
        ocrd_handler.setLevel(logging.DEBUG)
188
        for logger_name in ROOT_OCRD_LOGGERS:
189
            logging.getLogger(logger_name).addHandler(ocrd_handler)
190
        for logger_name, logger_level in LOGGING_DEFAULTS.items():
191
            logging.getLogger(logger_name).setLevel(logger_level)
192
    _initialized_flag = True
193
194
def disableLogging(silent=not config.OCRD_LOGGING_DEBUG):
195
    """
196
    Disables all logging of the ``ocrd`` logger and descendants
197
198
    Keyword Args:
199
        - silent (bool, True): Whether to log logging behavior by printing to stderr
200
    """
201
    global _initialized_flag # pylint: disable=global-statement
202
    if _initialized_flag and not silent:
203
        print("[LOGGING] Disabling logging", file=sys.stderr)
204
    _initialized_flag = False
205
    # logging.basicConfig(level=logging.CRITICAL)
206
    # logging.disable(logging.ERROR)
207
    # remove all handlers for the ocrd logger
208
    for logger_name in ROOT_OCRD_LOGGERS:
209
        for handler in logging.getLogger(logger_name).handlers[:]:
210
            logging.getLogger(logger_name).removeHandler(handler)
211
    for logger_name in LOGGING_DEFAULTS:
212
        logging.getLogger(logger_name).setLevel(logging.NOTSET)
213
214
# Initializing stream handlers at module level
215
# would cause message output in all runtime contexts,
216
# including those which are already run for std output
217
# (--dump-json, --version, ocrd-tool, bashlib etc).
218
# So this needs to be an opt-in from the CLIs/decorators:
219
#initLogging()
220
# Also, we even have to block log output for libraries
221
# (like matplotlib/tensorflow) which set up logging
222
# themselves already:
223
disableLogging()
224