Passed
Pull Request — master (#1080)
by Konstantin
02:29
created

PropagationShyLogger.removeHandler()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
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
40
__all__ = [
41
    'disableLogging',
42
    'getLevelName',
43
    'getLogger',
44
    'initLogging',
45
    'logging',
46
    'setOverrideLogLevel',
47
]
48
49
_initialized_flag = False
50
51
_ocrdLevel2pythonLevel = {
52
    'TRACE': 'DEBUG',
53
    'OFF': 'CRITICAL',
54
    'FATAL': 'ERROR',
55
}
56
57
def getLevelName(lvl):
58
    """
59
    Get (string) python logging level for (string) spec-defined log level name.
60
    """
61
    lvl = _ocrdLevel2pythonLevel.get(lvl, lvl)
62
    return logging.getLevelName(lvl)
63
64
def getLogger(*args, **kwargs):
65
    """
66
    Wrapper around ``logging.getLogger`` that alls :py:func:`initLogging` if
67
    that wasn't explicitly called before.
68
    """
69
    if not _initialized_flag:
70
        initLogging()
71
    logger = logging.getLogger(*args, **kwargs)
72
    return logger
73
74
def setOverrideLogLevel(lvl, silent=False):
75
    """
76
    Override the output log level of the handlers attached to the ``ocrd`` logger.
77
78
    Args:
79
        lvl (string): Log level name.
80
        silent (boolean): Whether to log the override call
81
    """
82
    if not _initialized_flag:
83
        initLogging()
84
    ocrd_logger = logging.getLogger('ocrd')
85
86
    if lvl is None:
87
        if not silent:
88
            ocrd_logger.info('Reset log level override')
89
        ocrd_logger.setLevel(logging.NOTSET)
90
    else:
91
        if not silent:
92
            ocrd_logger.info('Overriding log level globally to %s', lvl)
93
        ocrd_logger.setLevel(lvl)
94
95
def initLogging(builtin_only=False, basic_config=True, force_reinit=False):
96
    """
97
    Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig
98
99
    initLogging is to be called by OCR-D/core once, i.e.
100
        -  for the ``ocrd`` CLI
101
        -  for the processor wrapper methods
102
103
    Other processes that use OCR-D/core as a library can, but do not have to, use this functionality.
104
105
    Keyword Args:
106
        - basic_config (bool, False): Whether to attach the handler to the
107
                                      root logger instead of just the ``ocrd`` logger
108
                                      like ``logging.basicConfig`` does.
109
        - builtin_only (bool, False): Whether to search for logging configuration
110
                                      on-disk (``False``) or only use the
111
                                      hard-coded config (``True``). For testing
112
        - force_reinit (bool, False): Whether to ignore the module-level
113
                                      ``_initialized_flag``. For testing only.
114
    """
115
    global _initialized_flag
116
    if _initialized_flag and not force_reinit:
117
        return
118
119
    # https://docs.python.org/3/library/logging.html#logging.disable
120
    # If logging.disable(logging.NOTSET) is called, it effectively removes this
121
    # overriding level, so that logging output again depends on the effective
122
    # levels of individual loggers.
123
    logging.disable(logging.NOTSET)
124
125
    # remove all handlers for the ocrd logger
126
    for handler in logging.getLogger('ocrd').handlers[:]:
127
        logging.getLogger('ocrd').removeHandler(handler)
128
129
    config_file = None
130
    if not builtin_only:
131
        CONFIG_PATHS = [
132
            Path.cwd(),
133
            Path.home(),
134
            Path('/etc'),
135
        ]
136
        config_file = next((f for f \
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable f does not seem to be defined.
Loading history...
137
                in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \
138
                if f.exists()),
139
                None)
140
    if config_file:
141
        logging.config.fileConfig(config_file)
142
        logging.getLogger('ocrd.logging').debug("Picked up logging config at %s", config_file)
143
    else:
144
        # Default logging config
145
        ocrd_handler = logging.StreamHandler(stream=sys.stderr)
146
        ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT))
147
        if basic_config:
148
            logging.getLogger('').addHandler(ocrd_handler)
149
        else:
150
            logging.getLogger('ocrd').addHandler(ocrd_handler)
151
        logging.getLogger('ocrd').setLevel('INFO')
152
        #  logging.getLogger('ocrd.resolver').setLevel(logging.INFO)
153
        #  logging.getLogger('ocrd.resolver.download_to_directory').setLevel(logging.INFO)
154
        #  logging.getLogger('ocrd.resolver.add_files_to_mets').setLevel(logging.INFO)
155
        logging.getLogger('PIL').setLevel(logging.INFO)
156
        # To cut back on the `Self-intersection at or near point` INFO messages
157
        logging.getLogger('shapely.geos').setLevel(logging.ERROR)
158
        logging.getLogger('tensorflow').setLevel(logging.ERROR)
159
160
    _initialized_flag = True
161
162
def disableLogging():
163
    """
164
    Disables all logging of the ``ocrd`` logger and descendants
165
    """
166
    global _initialized_flag # pylint: disable=global-statement
167
    if _initialized_flag:
168
        logging.getLogger('ocrd.logging').debug("Disabling logging")
169
    _initialized_flag = False
170
    # logging.basicConfig(level=logging.CRITICAL)
171
    # logging.disable(logging.ERROR)
172
    # remove all handlers for the ocrd logger
173
    for handler in logging.getLogger('ocrd').handlers[:]:
174
        logging.getLogger('ocrd').removeHandler(handler)
175
176
# Initializing stream handlers at module level
177
# would cause message output in all runtime contexts,
178
# including those which are already run for std output
179
# (--dump-json, --version, ocrd-tool, bashlib etc).
180
# So this needs to be an opt-in from the CLIs/decorators:
181
#initLogging()
182
# Also, we even have to block log output for libraries
183
# (like matplotlib/tensorflow) which set up logging
184
# themselves already:
185
disableLogging()
186