beans-logging 6.0.3__py3-none-any.whl → 7.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- beans_logging/__init__.py +6 -6
- beans_logging/__version__.py +1 -1
- beans_logging/_builder.py +154 -0
- beans_logging/_constants.py +17 -5
- beans_logging/_core.py +295 -0
- beans_logging/_intercept.py +106 -0
- beans_logging/auto.py +3 -12
- beans_logging/config.py +118 -70
- beans_logging/filters.py +37 -20
- beans_logging/formats.py +20 -4
- beans_logging/{rotation.py → rotators.py} +20 -14
- beans_logging/schemas.py +143 -0
- beans_logging/sinks.py +11 -2
- {beans_logging-6.0.3.dist-info → beans_logging-7.0.0.dist-info}/METADATA +80 -61
- beans_logging-7.0.0.dist-info/RECORD +18 -0
- beans_logging/_base.py +0 -642
- beans_logging/_handlers.py +0 -40
- beans_logging/_utils.py +0 -101
- beans_logging-6.0.3.dist-info/RECORD +0 -17
- {beans_logging-6.0.3.dist-info → beans_logging-7.0.0.dist-info}/WHEEL +0 -0
- {beans_logging-6.0.3.dist-info → beans_logging-7.0.0.dist-info}/licenses/LICENSE.txt +0 -0
- {beans_logging-6.0.3.dist-info → beans_logging-7.0.0.dist-info}/top_level.txt +0 -0
beans_logging/_base.py
DELETED
|
@@ -1,642 +0,0 @@
|
|
|
1
|
-
# Standard libraries
|
|
2
|
-
import os
|
|
3
|
-
import copy
|
|
4
|
-
import json
|
|
5
|
-
import logging
|
|
6
|
-
from typing import Any
|
|
7
|
-
|
|
8
|
-
# Third-party libraries
|
|
9
|
-
import yaml
|
|
10
|
-
from loguru import logger
|
|
11
|
-
from loguru._logger import Logger
|
|
12
|
-
from pydantic import validate_call
|
|
13
|
-
|
|
14
|
-
# Internal modules
|
|
15
|
-
from ._utils import create_dir, deep_merge
|
|
16
|
-
from ._handlers import InterceptHandler
|
|
17
|
-
from .rotation import RotationChecker
|
|
18
|
-
from .config import LoggerConfigPM
|
|
19
|
-
from .sinks import std_sink
|
|
20
|
-
from .formats import json_format
|
|
21
|
-
from .filters import (
|
|
22
|
-
use_all_filter,
|
|
23
|
-
use_std_filter,
|
|
24
|
-
use_file_filter,
|
|
25
|
-
use_file_err_filter,
|
|
26
|
-
use_file_json_filter,
|
|
27
|
-
use_file_json_err_filter,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
class LoggerLoader:
|
|
32
|
-
"""LoggerLoader class for setting up loguru logger.
|
|
33
|
-
|
|
34
|
-
Attributes:
|
|
35
|
-
_CONFIG_FILE_PATH (str ): Default logger config file path. Defaults to '${PWD}/configs/logger.yml'.
|
|
36
|
-
|
|
37
|
-
handlers_map (dict ): Registered logger handlers map as dictionary. Defaults to None.
|
|
38
|
-
config (LoggerConfigPM): Logger config as <class 'LoggerConfigPM'>. Defaults to None.
|
|
39
|
-
config_file_path (str ): Logger config file path. Defaults to `LoggerLoader._CONFIG_FILE_PATH`.
|
|
40
|
-
|
|
41
|
-
Methods:
|
|
42
|
-
load() : Load logger handlers based on logger config.
|
|
43
|
-
remove_handler() : Remove all handlers or specific handler by name or id from logger.
|
|
44
|
-
update_config() : Update logger config with new config.
|
|
45
|
-
_load_env_vars() : Load 'BEANS_LOGGING_CONFIG_PATH' environment variable for logger config file path.
|
|
46
|
-
_load_config_file() : Load logger config from file.
|
|
47
|
-
_check_env() : Check environment variables for logger config.
|
|
48
|
-
_check_config() : Check logger config to update some options before loading handlers.
|
|
49
|
-
_add_stream_std_handler() : Add std stream handler to logger.
|
|
50
|
-
_add_file_log_handler() : Add log file handler to logger.
|
|
51
|
-
_add_file_err_handler() : Add error log file handler to logger.
|
|
52
|
-
_add_file_json_handler() : Add json log file handler to logger.
|
|
53
|
-
_add_file_json_err_handler(): Add json error log file handler to logger.
|
|
54
|
-
add_custom_handler() : Add custom handler to logger.
|
|
55
|
-
_load_intercept_handlers() : Load intercept handlers to catch third-pary modules log or mute them.
|
|
56
|
-
"""
|
|
57
|
-
|
|
58
|
-
_CONFIG_FILE_PATH = os.path.join(os.getcwd(), "configs", "logger.yml")
|
|
59
|
-
|
|
60
|
-
@validate_call
|
|
61
|
-
def __init__(
|
|
62
|
-
self,
|
|
63
|
-
config: LoggerConfigPM | dict[str, Any] | None = None,
|
|
64
|
-
config_file_path: str = _CONFIG_FILE_PATH,
|
|
65
|
-
auto_config_file: bool = True,
|
|
66
|
-
auto_load: bool = False,
|
|
67
|
-
):
|
|
68
|
-
"""LoggerLoader constructor method.
|
|
69
|
-
|
|
70
|
-
Args:
|
|
71
|
-
config (LoggerConfigPM | dict | None, optional): New logger config to update loaded config.
|
|
72
|
-
Defaults to None.
|
|
73
|
-
config_file_path (str , optional): Logger config file path. Defaults to
|
|
74
|
-
`LoggerLoader._CONFIG_FILE_PATH`.
|
|
75
|
-
auto_config_file (bool , optional): Indicates whether to load logger config
|
|
76
|
-
file or not. Defaults to True.
|
|
77
|
-
auto_load (bool , optional): Indicates whether to load logger
|
|
78
|
-
handlers or not. Defaults to False.
|
|
79
|
-
"""
|
|
80
|
-
|
|
81
|
-
self.handlers_map = {"default": 0}
|
|
82
|
-
self.config = LoggerConfigPM()
|
|
83
|
-
if config:
|
|
84
|
-
self.update_config(config=config)
|
|
85
|
-
self.config_file_path = config_file_path
|
|
86
|
-
|
|
87
|
-
self._load_env_vars()
|
|
88
|
-
|
|
89
|
-
if auto_config_file:
|
|
90
|
-
self._load_config_file()
|
|
91
|
-
|
|
92
|
-
if auto_load:
|
|
93
|
-
self.load()
|
|
94
|
-
|
|
95
|
-
def load(self) -> Logger:
|
|
96
|
-
"""Load logger handlers based on logger config.
|
|
97
|
-
|
|
98
|
-
Returns:
|
|
99
|
-
Logger: Main loguru logger instance.
|
|
100
|
-
"""
|
|
101
|
-
|
|
102
|
-
self.remove_handler()
|
|
103
|
-
|
|
104
|
-
self._check_env()
|
|
105
|
-
self._check_config()
|
|
106
|
-
|
|
107
|
-
if self.config.stream.std_handler.enabled:
|
|
108
|
-
self._add_stream_std_handler()
|
|
109
|
-
|
|
110
|
-
if self.config.file.log_handlers.enabled:
|
|
111
|
-
self._add_file_log_handler()
|
|
112
|
-
self._add_file_err_handler()
|
|
113
|
-
|
|
114
|
-
if self.config.file.json_handlers.enabled:
|
|
115
|
-
self._add_file_json_handler()
|
|
116
|
-
self._add_file_json_err_handler()
|
|
117
|
-
|
|
118
|
-
self._load_intercept_handlers()
|
|
119
|
-
|
|
120
|
-
return logger
|
|
121
|
-
|
|
122
|
-
@validate_call
|
|
123
|
-
def remove_handler(self, handler: str | None = None, handler_type: str = "NAME"):
|
|
124
|
-
"""Remove all handlers or specific handler by name or id from logger.
|
|
125
|
-
|
|
126
|
-
Raises:
|
|
127
|
-
ValueError: The `handler_type` argument value '{handler_type}' is invalid, must be 'NAME' or 'ID'!
|
|
128
|
-
|
|
129
|
-
Args:
|
|
130
|
-
handler (str, optional): Handler name or id to remove. Defaults to None.
|
|
131
|
-
handler_type (int, optional): Handler type to remove, must be 'NAME' or 'ID'. Defaults to 'name'.
|
|
132
|
-
"""
|
|
133
|
-
|
|
134
|
-
if handler:
|
|
135
|
-
handler_type = handler_type.strip().upper()
|
|
136
|
-
if handler_type == "NAME":
|
|
137
|
-
if handler in self.handlers_map:
|
|
138
|
-
_handler_id = self.handlers_map[handler]
|
|
139
|
-
logger.remove(_handler_id)
|
|
140
|
-
self.handlers_map.pop(handler)
|
|
141
|
-
return
|
|
142
|
-
elif handler_type == "ID":
|
|
143
|
-
if handler in self.handlers_map.values():
|
|
144
|
-
logger.remove(handler)
|
|
145
|
-
for _handler_name, _handler_id in self.handlers_map.items():
|
|
146
|
-
if handler == _handler_id:
|
|
147
|
-
self.handlers_map.pop(_handler_name)
|
|
148
|
-
return
|
|
149
|
-
else:
|
|
150
|
-
raise ValueError(
|
|
151
|
-
f"`handler_type` argument value '{handler_type}' is invalid, must be 'NAME' or 'ID'!"
|
|
152
|
-
)
|
|
153
|
-
|
|
154
|
-
logger.remove()
|
|
155
|
-
self.handlers_map.clear()
|
|
156
|
-
|
|
157
|
-
@validate_call
|
|
158
|
-
def update_config(self, config: LoggerConfigPM | dict[str, Any]):
|
|
159
|
-
"""Update logger config with new config.
|
|
160
|
-
|
|
161
|
-
Args:
|
|
162
|
-
config (Union[LoggerConfigPM, dict], required): New logger config to update loaded config.
|
|
163
|
-
|
|
164
|
-
Raises:
|
|
165
|
-
Exception: Failed to load `config` argument into <class 'LoggerConfigPM'>.
|
|
166
|
-
"""
|
|
167
|
-
|
|
168
|
-
if isinstance(config, dict):
|
|
169
|
-
_config_dict = self.config.model_dump()
|
|
170
|
-
_merged_dict = deep_merge(_config_dict, config)
|
|
171
|
-
try:
|
|
172
|
-
self.config = LoggerConfigPM(**_merged_dict)
|
|
173
|
-
except Exception:
|
|
174
|
-
logger.critical(
|
|
175
|
-
"Failed to load `config` argument into <class 'LoggerConfigPM'>."
|
|
176
|
-
)
|
|
177
|
-
raise
|
|
178
|
-
|
|
179
|
-
elif isinstance(config, LoggerConfigPM):
|
|
180
|
-
self.config = config
|
|
181
|
-
|
|
182
|
-
def _load_env_vars(self):
|
|
183
|
-
"""Load 'BEANS_LOGGING_CONFIG_PATH' environment variable for logger config file path."""
|
|
184
|
-
|
|
185
|
-
_env_config_file_path = os.getenv("BEANS_LOGGING_CONFIG_PATH")
|
|
186
|
-
if _env_config_file_path:
|
|
187
|
-
try:
|
|
188
|
-
self.config_file_path = _env_config_file_path
|
|
189
|
-
except Exception:
|
|
190
|
-
logger.warning(
|
|
191
|
-
"Failed to load 'BEANS_LOGGING_CONFIG_PATH' environment variable!"
|
|
192
|
-
)
|
|
193
|
-
|
|
194
|
-
def _load_config_file(self):
|
|
195
|
-
"""Load logger config from file."""
|
|
196
|
-
|
|
197
|
-
_file_format = ""
|
|
198
|
-
if self.config_file_path.lower().endswith((".yml", ".yaml")):
|
|
199
|
-
_file_format = "YAML"
|
|
200
|
-
if self.config_file_path.lower().endswith(".json"):
|
|
201
|
-
_file_format = "JSON"
|
|
202
|
-
# elif self.config_file_path.lower().endswith(".toml"):
|
|
203
|
-
# _file_format = "TOML"
|
|
204
|
-
|
|
205
|
-
# Loading config from file, if it's exits:
|
|
206
|
-
if os.path.isfile(self.config_file_path):
|
|
207
|
-
if _file_format == "YAML":
|
|
208
|
-
try:
|
|
209
|
-
with open(self.config_file_path, encoding="utf-8") as _config_file:
|
|
210
|
-
_new_config_dict = yaml.safe_load(_config_file) or {}
|
|
211
|
-
if "logger" not in _new_config_dict:
|
|
212
|
-
logger.warning(
|
|
213
|
-
f"'{self.config_file_path}' YAML config file doesn't have 'logger' section!"
|
|
214
|
-
)
|
|
215
|
-
return
|
|
216
|
-
|
|
217
|
-
_new_config_dict = _new_config_dict["logger"]
|
|
218
|
-
_config_dict = self.config.model_dump()
|
|
219
|
-
|
|
220
|
-
_merged_dict = deep_merge(_config_dict, _new_config_dict)
|
|
221
|
-
self.config = LoggerConfigPM(**_merged_dict)
|
|
222
|
-
except Exception:
|
|
223
|
-
logger.critical(
|
|
224
|
-
f"Failed to load '{self.config_file_path}' yaml config file."
|
|
225
|
-
)
|
|
226
|
-
raise
|
|
227
|
-
elif _file_format == "JSON":
|
|
228
|
-
try:
|
|
229
|
-
with open(self.config_file_path, encoding="utf-8") as _config_file:
|
|
230
|
-
_new_config_dict = json.load(_config_file) or {}
|
|
231
|
-
if "logger" not in _new_config_dict:
|
|
232
|
-
logger.warning(
|
|
233
|
-
f"'{self.config_file_path}' JSON config file doesn't have 'logger' section!"
|
|
234
|
-
)
|
|
235
|
-
return
|
|
236
|
-
|
|
237
|
-
_new_config_dict = _new_config_dict["logger"]
|
|
238
|
-
_config_dict = self.config.model_dump()
|
|
239
|
-
|
|
240
|
-
_merged_dict = deep_merge(_config_dict, _new_config_dict)
|
|
241
|
-
self.config = LoggerConfigPM(**_merged_dict)
|
|
242
|
-
except Exception:
|
|
243
|
-
logger.critical(
|
|
244
|
-
f"Failed to load '{self.config_file_path}' json config file."
|
|
245
|
-
)
|
|
246
|
-
raise
|
|
247
|
-
# elif _file_format == "TOML":
|
|
248
|
-
# try:
|
|
249
|
-
# import toml
|
|
250
|
-
|
|
251
|
-
# with open(
|
|
252
|
-
# self.config_file_path, "r", encoding="utf-8"
|
|
253
|
-
# ) as _config_file:
|
|
254
|
-
# _new_config_dict = toml.load(_config_file) or {}
|
|
255
|
-
# if "logger" not in _new_config_dict:
|
|
256
|
-
# logger.warning(
|
|
257
|
-
# f"'{self.config_file_path}' TOML config file doesn't have 'logger' section!"
|
|
258
|
-
# )
|
|
259
|
-
# return
|
|
260
|
-
|
|
261
|
-
# _new_config_dict = _new_config_dict["logger"]
|
|
262
|
-
# _config_dict = self.config.model_dump()
|
|
263
|
-
|
|
264
|
-
# _merged_dict = deep_merge(_config_dict, _new_config_dict)
|
|
265
|
-
# self.config = LoggerConfigPM(**_merged_dict)
|
|
266
|
-
# except Exception:
|
|
267
|
-
# logger.critical(
|
|
268
|
-
# f"Failed to load '{self.config_file_path}' toml config file."
|
|
269
|
-
# )
|
|
270
|
-
# raise
|
|
271
|
-
|
|
272
|
-
def _check_env(self):
|
|
273
|
-
"""Check environment variables for logger config."""
|
|
274
|
-
|
|
275
|
-
# Checking environment for DEBUG option:
|
|
276
|
-
_is_debug = False
|
|
277
|
-
_ENV = str(os.getenv("ENV")).strip().lower()
|
|
278
|
-
_DEBUG = str(os.getenv("DEBUG")).strip().lower()
|
|
279
|
-
if (
|
|
280
|
-
(_DEBUG == "true")
|
|
281
|
-
or (_DEBUG == "1")
|
|
282
|
-
or ((_ENV == "development") and ((_DEBUG == "none") or (_DEBUG == "")))
|
|
283
|
-
):
|
|
284
|
-
_is_debug = True
|
|
285
|
-
|
|
286
|
-
if _is_debug and (self.config.level != "TRACE"):
|
|
287
|
-
self.config.level = "DEBUG"
|
|
288
|
-
|
|
289
|
-
if "BEANS_LOGGING_LOGS_DIR" in os.environ:
|
|
290
|
-
self.config.file.logs_dir = os.getenv("BEANS_LOGGING_LOGS_DIR")
|
|
291
|
-
|
|
292
|
-
# if self.config.stream.use_color:
|
|
293
|
-
# # Checking terminal could support xterm colors:
|
|
294
|
-
# _TERM = str(os.getenv("TERM")).strip()
|
|
295
|
-
# if not "xterm" in _TERM:
|
|
296
|
-
# self.config.stream.use_color = False
|
|
297
|
-
|
|
298
|
-
def _check_config(self):
|
|
299
|
-
"""Check logger config to update some options before loading handlers."""
|
|
300
|
-
|
|
301
|
-
if self.config.level == "TRACE":
|
|
302
|
-
self.config.use_diagnose = True
|
|
303
|
-
|
|
304
|
-
if self.config.stream.use_icon:
|
|
305
|
-
self.config.stream.format_str = self.config.stream.format_str.replace(
|
|
306
|
-
"level_short:<5", "level.icon:<4"
|
|
307
|
-
)
|
|
308
|
-
|
|
309
|
-
if not os.path.isabs(self.config.file.logs_dir):
|
|
310
|
-
self.config.file.logs_dir = os.path.join(
|
|
311
|
-
os.getcwd(), self.config.file.logs_dir
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
if "{app_name}" in self.config.file.log_handlers.log_path:
|
|
315
|
-
self.config.file.log_handlers.log_path = (
|
|
316
|
-
self.config.file.log_handlers.log_path.format(
|
|
317
|
-
app_name=self.config.app_name
|
|
318
|
-
)
|
|
319
|
-
)
|
|
320
|
-
|
|
321
|
-
if "{app_name}" in self.config.file.log_handlers.err_path:
|
|
322
|
-
self.config.file.log_handlers.err_path = (
|
|
323
|
-
self.config.file.log_handlers.err_path.format(
|
|
324
|
-
app_name=self.config.app_name
|
|
325
|
-
)
|
|
326
|
-
)
|
|
327
|
-
|
|
328
|
-
if "{app_name}" in self.config.file.json_handlers.log_path:
|
|
329
|
-
self.config.file.json_handlers.log_path = (
|
|
330
|
-
self.config.file.json_handlers.log_path.format(
|
|
331
|
-
app_name=self.config.app_name
|
|
332
|
-
)
|
|
333
|
-
)
|
|
334
|
-
|
|
335
|
-
if "{app_name}" in self.config.file.json_handlers.err_path:
|
|
336
|
-
self.config.file.json_handlers.err_path = (
|
|
337
|
-
self.config.file.json_handlers.err_path.format(
|
|
338
|
-
app_name=self.config.app_name
|
|
339
|
-
)
|
|
340
|
-
)
|
|
341
|
-
|
|
342
|
-
def _add_stream_std_handler(self) -> int:
|
|
343
|
-
"""Add std stream handler to logger.
|
|
344
|
-
|
|
345
|
-
Returns:
|
|
346
|
-
int: Handler id.
|
|
347
|
-
"""
|
|
348
|
-
|
|
349
|
-
return self.add_custom_handler(handler_name="STREAM.STD", filter=use_std_filter)
|
|
350
|
-
|
|
351
|
-
def _add_file_log_handler(self) -> int:
|
|
352
|
-
"""Add log file handler to logger.
|
|
353
|
-
|
|
354
|
-
Returns:
|
|
355
|
-
int: Handler id.
|
|
356
|
-
"""
|
|
357
|
-
|
|
358
|
-
return self.add_custom_handler(handler_name="FILE", filter=use_file_filter)
|
|
359
|
-
|
|
360
|
-
def _add_file_err_handler(self) -> int:
|
|
361
|
-
"""Add error log file handler to logger.
|
|
362
|
-
|
|
363
|
-
Returns:
|
|
364
|
-
int: Handler id.
|
|
365
|
-
"""
|
|
366
|
-
|
|
367
|
-
_handler_id = self.add_custom_handler(
|
|
368
|
-
handler_name="FILE_ERR",
|
|
369
|
-
sink=self.config.file.log_handlers.err_path,
|
|
370
|
-
level="WARNING",
|
|
371
|
-
filter=use_file_err_filter,
|
|
372
|
-
)
|
|
373
|
-
return _handler_id
|
|
374
|
-
|
|
375
|
-
def _add_file_json_handler(self) -> int:
|
|
376
|
-
"""Add json log file handler to logger.
|
|
377
|
-
|
|
378
|
-
Returns:
|
|
379
|
-
int: Handler id.
|
|
380
|
-
"""
|
|
381
|
-
|
|
382
|
-
_kwargs = {
|
|
383
|
-
"sink": self.config.file.json_handlers.log_path,
|
|
384
|
-
"filter": use_file_json_filter,
|
|
385
|
-
"serialize": True,
|
|
386
|
-
}
|
|
387
|
-
if self.config.file.json_handlers.use_custom:
|
|
388
|
-
_kwargs["format"] = json_format
|
|
389
|
-
_kwargs["serialize"] = False
|
|
390
|
-
|
|
391
|
-
_handler_id = self.add_custom_handler(handler_name="FILE.JSON", **_kwargs)
|
|
392
|
-
return _handler_id
|
|
393
|
-
|
|
394
|
-
def _add_file_json_err_handler(self) -> int:
|
|
395
|
-
"""Add json error log file handler to logger.
|
|
396
|
-
|
|
397
|
-
Returns:
|
|
398
|
-
int: Handler id.
|
|
399
|
-
"""
|
|
400
|
-
|
|
401
|
-
_kwargs = {
|
|
402
|
-
"sink": self.config.file.json_handlers.err_path,
|
|
403
|
-
"level": "WARNING",
|
|
404
|
-
"filter": use_file_json_err_filter,
|
|
405
|
-
"serialize": True,
|
|
406
|
-
}
|
|
407
|
-
if self.config.file.json_handlers.use_custom:
|
|
408
|
-
_kwargs["format"] = json_format
|
|
409
|
-
_kwargs["serialize"] = False
|
|
410
|
-
|
|
411
|
-
_handler_id = self.add_custom_handler(
|
|
412
|
-
handler_name="FILE.JSON_ERR",
|
|
413
|
-
**_kwargs,
|
|
414
|
-
)
|
|
415
|
-
return _handler_id
|
|
416
|
-
|
|
417
|
-
@validate_call
|
|
418
|
-
def add_custom_handler(self, handler_name: str, **kwargs) -> int:
|
|
419
|
-
"""Add custom handler to logger.
|
|
420
|
-
|
|
421
|
-
Args:
|
|
422
|
-
handler_name (str): Handler name/type to add logger.
|
|
423
|
-
|
|
424
|
-
Raises:
|
|
425
|
-
ValueError: Custom handler '{handler_name}' already exists in logger!
|
|
426
|
-
ValueError: The `sink` argument is required for custom handler!
|
|
427
|
-
|
|
428
|
-
Returns:
|
|
429
|
-
int: Handler id.
|
|
430
|
-
"""
|
|
431
|
-
|
|
432
|
-
if handler_name in self.handlers_map:
|
|
433
|
-
raise ValueError(
|
|
434
|
-
f"Custom handler '{handler_name}' already exists in logger!"
|
|
435
|
-
)
|
|
436
|
-
|
|
437
|
-
_handler_id = None
|
|
438
|
-
try:
|
|
439
|
-
handler_name = handler_name.strip().upper()
|
|
440
|
-
|
|
441
|
-
if "level" not in kwargs:
|
|
442
|
-
kwargs["level"] = self.config.level
|
|
443
|
-
|
|
444
|
-
if "filter" not in kwargs:
|
|
445
|
-
kwargs["filter"] = use_all_filter
|
|
446
|
-
|
|
447
|
-
if "backtrace" not in kwargs:
|
|
448
|
-
kwargs["backtrace"] = self.config.use_backtrace
|
|
449
|
-
|
|
450
|
-
if "diagnose" not in kwargs:
|
|
451
|
-
kwargs["diagnose"] = self.config.use_diagnose
|
|
452
|
-
|
|
453
|
-
if handler_name.startswith("STREAM"):
|
|
454
|
-
if "sink" not in kwargs:
|
|
455
|
-
kwargs["sink"] = std_sink
|
|
456
|
-
|
|
457
|
-
if "format" not in kwargs:
|
|
458
|
-
kwargs["format"] = self.config.stream.format_str
|
|
459
|
-
|
|
460
|
-
if "colorize" not in kwargs:
|
|
461
|
-
kwargs["colorize"] = self.config.stream.use_color
|
|
462
|
-
elif handler_name.startswith("FILE"):
|
|
463
|
-
kwargs["enqueue"] = True
|
|
464
|
-
|
|
465
|
-
if "sink" not in kwargs:
|
|
466
|
-
kwargs["sink"] = self.config.file.log_handlers.log_path
|
|
467
|
-
|
|
468
|
-
if isinstance(kwargs["sink"], str):
|
|
469
|
-
_log_path = kwargs["sink"]
|
|
470
|
-
if not os.path.isabs(_log_path):
|
|
471
|
-
_log_path = os.path.abspath(
|
|
472
|
-
os.path.join(self.config.file.logs_dir, _log_path)
|
|
473
|
-
)
|
|
474
|
-
|
|
475
|
-
if "{app_name}" in _log_path:
|
|
476
|
-
_log_path = _log_path.format(app_name=self.config.app_name)
|
|
477
|
-
|
|
478
|
-
_logs_dir, _ = os.path.split(_log_path)
|
|
479
|
-
create_dir(create_dir=_logs_dir)
|
|
480
|
-
kwargs["sink"] = _log_path
|
|
481
|
-
|
|
482
|
-
if "format" not in kwargs:
|
|
483
|
-
kwargs["format"] = self.config.file.log_handlers.format_str
|
|
484
|
-
|
|
485
|
-
if "rotation" not in kwargs:
|
|
486
|
-
kwargs["rotation"] = RotationChecker(
|
|
487
|
-
rotate_size=self.config.file.rotate_size,
|
|
488
|
-
rotate_time=self.config.file.rotate_time,
|
|
489
|
-
).should_rotate
|
|
490
|
-
|
|
491
|
-
if "retention" not in kwargs:
|
|
492
|
-
kwargs["retention"] = self.config.file.backup_count
|
|
493
|
-
|
|
494
|
-
if "encoding" not in kwargs:
|
|
495
|
-
kwargs["encoding"] = self.config.file.encoding
|
|
496
|
-
|
|
497
|
-
if "sink" not in kwargs:
|
|
498
|
-
raise ValueError(
|
|
499
|
-
f"`sink` argument is required for custom handler '{handler_name}'!"
|
|
500
|
-
)
|
|
501
|
-
|
|
502
|
-
_handler_id = logger.add(**kwargs)
|
|
503
|
-
except Exception:
|
|
504
|
-
logger.critical(f"Failed to add custom handler '{handler_name}' to logger!")
|
|
505
|
-
raise
|
|
506
|
-
|
|
507
|
-
self.handlers_map[handler_name] = _handler_id
|
|
508
|
-
return _handler_id
|
|
509
|
-
|
|
510
|
-
def _load_intercept_handlers(self):
|
|
511
|
-
"""Load intercept handlers to catch third-pary modules log or mute them."""
|
|
512
|
-
|
|
513
|
-
_intercept_handler = InterceptHandler()
|
|
514
|
-
|
|
515
|
-
# Intercepting all logs from standard (root logger) logging:
|
|
516
|
-
logging.basicConfig(handlers=[_intercept_handler], level=0, force=True)
|
|
517
|
-
|
|
518
|
-
_intercepted_modules = set()
|
|
519
|
-
_muted_modules = set()
|
|
520
|
-
|
|
521
|
-
if self.config.intercept.auto_load.enabled:
|
|
522
|
-
for _module_name in list(logging.root.manager.loggerDict.keys()):
|
|
523
|
-
if self.config.intercept.auto_load.only_base:
|
|
524
|
-
_module_name = _module_name.split(".")[0]
|
|
525
|
-
|
|
526
|
-
if (_module_name not in _intercepted_modules) and (
|
|
527
|
-
_module_name not in self.config.intercept.auto_load.ignore_modules
|
|
528
|
-
):
|
|
529
|
-
_logger = logging.getLogger(_module_name)
|
|
530
|
-
_logger.handlers = [_intercept_handler]
|
|
531
|
-
_logger.propagate = False
|
|
532
|
-
_intercepted_modules.add(_module_name)
|
|
533
|
-
|
|
534
|
-
for _include_module_name in self.config.intercept.include_modules:
|
|
535
|
-
_logger = logging.getLogger(_include_module_name)
|
|
536
|
-
_logger.handlers = [_intercept_handler]
|
|
537
|
-
logger.propagate = False
|
|
538
|
-
|
|
539
|
-
if _include_module_name not in _intercepted_modules:
|
|
540
|
-
_intercepted_modules.add(_include_module_name)
|
|
541
|
-
|
|
542
|
-
for _mute_module_name in self.config.intercept.mute_modules:
|
|
543
|
-
_logger = logging.getLogger(_mute_module_name)
|
|
544
|
-
_logger.handlers = []
|
|
545
|
-
_logger.propagate = False
|
|
546
|
-
_logger.disabled = True
|
|
547
|
-
|
|
548
|
-
if _mute_module_name in _intercepted_modules:
|
|
549
|
-
_intercepted_modules.remove(_mute_module_name)
|
|
550
|
-
|
|
551
|
-
if _mute_module_name not in _muted_modules:
|
|
552
|
-
_muted_modules.add(_mute_module_name)
|
|
553
|
-
|
|
554
|
-
logger.trace(
|
|
555
|
-
f"Intercepted modules: {list(_intercepted_modules)}; Muted modules: {list(_muted_modules)};"
|
|
556
|
-
)
|
|
557
|
-
|
|
558
|
-
# ATTRIBUTES #
|
|
559
|
-
# handlers_map
|
|
560
|
-
@property
|
|
561
|
-
def handlers_map(self) -> dict[str, int]:
|
|
562
|
-
try:
|
|
563
|
-
return self.__handlers_map
|
|
564
|
-
except AttributeError:
|
|
565
|
-
self.__handlers_map = {"default": 0}
|
|
566
|
-
|
|
567
|
-
return self.__handlers_map
|
|
568
|
-
|
|
569
|
-
@handlers_map.setter
|
|
570
|
-
def handlers_map(self, handlers_map: dict[str, int]):
|
|
571
|
-
if not isinstance(handlers_map, dict):
|
|
572
|
-
raise TypeError(
|
|
573
|
-
f"`handlers_map` attribute type {type(handlers_map)} is invalid, must be <dict>!."
|
|
574
|
-
)
|
|
575
|
-
|
|
576
|
-
self.__handlers_map = copy.deepcopy(handlers_map)
|
|
577
|
-
|
|
578
|
-
# handlers_map
|
|
579
|
-
|
|
580
|
-
# config
|
|
581
|
-
@property
|
|
582
|
-
def config(self) -> LoggerConfigPM:
|
|
583
|
-
try:
|
|
584
|
-
return self.__config
|
|
585
|
-
except AttributeError:
|
|
586
|
-
self.__config = LoggerConfigPM()
|
|
587
|
-
|
|
588
|
-
return self.__config
|
|
589
|
-
|
|
590
|
-
@config.setter
|
|
591
|
-
def config(self, config: LoggerConfigPM):
|
|
592
|
-
if not isinstance(config, LoggerConfigPM):
|
|
593
|
-
raise TypeError(
|
|
594
|
-
f"`config` attribute type {type(config)} is invalid, must be a <class 'LoggerConfigPM'>!"
|
|
595
|
-
)
|
|
596
|
-
|
|
597
|
-
self.__config = copy.deepcopy(config)
|
|
598
|
-
|
|
599
|
-
# config
|
|
600
|
-
|
|
601
|
-
# config_file_path
|
|
602
|
-
@property
|
|
603
|
-
def config_file_path(self) -> str:
|
|
604
|
-
try:
|
|
605
|
-
return self.__config_file_path
|
|
606
|
-
except AttributeError:
|
|
607
|
-
self.__config_file_path = LoggerLoader._CONFIG_FILE_PATH
|
|
608
|
-
|
|
609
|
-
return self.__config_file_path
|
|
610
|
-
|
|
611
|
-
@config_file_path.setter
|
|
612
|
-
def config_file_path(self, config_file_path: str):
|
|
613
|
-
if not isinstance(config_file_path, str):
|
|
614
|
-
raise TypeError(
|
|
615
|
-
f"`config_file_path` attribute type {type(config_file_path)} is invalid, must be a <str>!"
|
|
616
|
-
)
|
|
617
|
-
|
|
618
|
-
config_file_path = config_file_path.strip()
|
|
619
|
-
if config_file_path == "":
|
|
620
|
-
raise ValueError("`config_file_path` attribute value is empty!")
|
|
621
|
-
|
|
622
|
-
if (not config_file_path.lower().endswith((".yml", ".yaml"))) and (
|
|
623
|
-
not config_file_path.lower().endswith(".json")
|
|
624
|
-
):
|
|
625
|
-
if not config_file_path.lower().endswith(".toml"):
|
|
626
|
-
raise NotImplementedError(
|
|
627
|
-
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
|
|
628
|
-
f"TOML file format is not supported yet!"
|
|
629
|
-
)
|
|
630
|
-
|
|
631
|
-
raise ValueError(
|
|
632
|
-
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
|
|
633
|
-
f"file must be '.yml', '.yaml' or '.json' format!"
|
|
634
|
-
)
|
|
635
|
-
|
|
636
|
-
if not os.path.isabs(config_file_path):
|
|
637
|
-
config_file_path = os.path.join(os.getcwd(), config_file_path)
|
|
638
|
-
|
|
639
|
-
self.__config_file_path = config_file_path
|
|
640
|
-
|
|
641
|
-
# config_file_path
|
|
642
|
-
# ATTRIBUTES #
|
beans_logging/_handlers.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import sys
|
|
2
|
-
import logging
|
|
3
|
-
from logging import LogRecord
|
|
4
|
-
|
|
5
|
-
from loguru import logger
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class InterceptHandler(logging.Handler):
|
|
9
|
-
"""A handler class that intercepts logs from standard logging and redirects them to loguru logger.
|
|
10
|
-
|
|
11
|
-
Inherits:
|
|
12
|
-
logging.Handler: Handler class from standard logging.
|
|
13
|
-
|
|
14
|
-
Overrides:
|
|
15
|
-
emit(): Handle intercepted log record.
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
def emit(self, record: LogRecord):
|
|
19
|
-
"""
|
|
20
|
-
Handle intercepted log record.
|
|
21
|
-
|
|
22
|
-
Args:
|
|
23
|
-
record (LogRecord, required): Log needs to be handled.
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
# Get corresponding Loguru level if it exists
|
|
27
|
-
try:
|
|
28
|
-
_level = logger.level(record.levelname).name
|
|
29
|
-
except ValueError:
|
|
30
|
-
_level = record.levelno
|
|
31
|
-
|
|
32
|
-
# Find caller from where originated the logged message
|
|
33
|
-
_frame, _depth = sys._getframe(6), 6
|
|
34
|
-
while _frame and _frame.f_code.co_filename == logging.__file__:
|
|
35
|
-
_frame = _frame.f_back
|
|
36
|
-
_depth += 1
|
|
37
|
-
|
|
38
|
-
logger.opt(depth=_depth, exception=record.exc_info).log(
|
|
39
|
-
_level, record.getMessage()
|
|
40
|
-
)
|