Passed
Pull Request — master (#1214)
by Konstantin
03:55
created

ocrd_utils.logging.initLogging()   F

Complexity

Conditions 16

Size

Total Lines 72
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

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