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