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