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