Passed
Push — master ( 66bb7b...57c197 )
by Konstantin
02:49
created

ocrd_utils.logging.initLogging()   D

Complexity

Conditions 13

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

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