beans-logging 6.0.0__py3-none-any.whl → 6.0.2__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 +0 -2
- beans_logging/__version__.py +1 -3
- beans_logging/_base.py +37 -41
- beans_logging/_consts.py +0 -2
- beans_logging/_handlers.py +2 -4
- beans_logging/_utils.py +2 -3
- beans_logging/auto.py +2 -3
- beans_logging/filters.py +0 -3
- beans_logging/formats.py +0 -2
- beans_logging/rotation.py +2 -4
- beans_logging/schemas.py +7 -7
- beans_logging/sinks.py +0 -2
- beans_logging-6.0.2.dist-info/METADATA +424 -0
- beans_logging-6.0.2.dist-info/RECORD +17 -0
- {beans_logging-6.0.0.dist-info → beans_logging-6.0.2.dist-info}/WHEEL +1 -1
- {beans_logging-6.0.0.dist-info → beans_logging-6.0.2.dist-info/licenses}/LICENSE.txt +1 -1
- {beans_logging-6.0.0.dist-info → beans_logging-6.0.2.dist-info}/top_level.txt +0 -1
- beans_logging-6.0.0.dist-info/METADATA +0 -326
- beans_logging-6.0.0.dist-info/RECORD +0 -20
- tests/__init__.py +0 -1
- tests/conftest.py +0 -16
- tests/test_beans_logging.py +0 -67
beans_logging/__init__.py
CHANGED
beans_logging/__version__.py
CHANGED
beans_logging/_base.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
## Standard libraries
|
|
1
|
+
# Standard libraries
|
|
4
2
|
import os
|
|
5
3
|
import copy
|
|
6
4
|
import json
|
|
7
5
|
import logging
|
|
8
|
-
from typing import
|
|
6
|
+
from typing import Any
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
# Third-party libraries
|
|
11
9
|
import yaml
|
|
12
10
|
from loguru import logger
|
|
13
11
|
from loguru._logger import Logger
|
|
@@ -18,7 +16,7 @@ if "2.0.0" <= pydantic.__version__:
|
|
|
18
16
|
else:
|
|
19
17
|
from pydantic import validate_arguments as validate_call
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
# Internal modules
|
|
22
20
|
from ._utils import create_dir, deep_merge
|
|
23
21
|
from ._handlers import InterceptHandler
|
|
24
22
|
from .rotation import RotationChecker
|
|
@@ -67,7 +65,7 @@ class LoggerLoader:
|
|
|
67
65
|
@validate_call
|
|
68
66
|
def __init__(
|
|
69
67
|
self,
|
|
70
|
-
config:
|
|
68
|
+
config: LoggerConfigPM | dict[str, Any] | None = None,
|
|
71
69
|
config_file_path: str = _CONFIG_FILE_PATH,
|
|
72
70
|
auto_config_file: bool = True,
|
|
73
71
|
auto_load: bool = False,
|
|
@@ -75,12 +73,14 @@ class LoggerLoader:
|
|
|
75
73
|
"""LoggerLoader constructor method.
|
|
76
74
|
|
|
77
75
|
Args:
|
|
78
|
-
config (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
auto_config_file (bool
|
|
83
|
-
|
|
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
84
|
"""
|
|
85
85
|
|
|
86
86
|
self.handlers_map = {"default": 0}
|
|
@@ -125,17 +125,15 @@ class LoggerLoader:
|
|
|
125
125
|
return logger
|
|
126
126
|
|
|
127
127
|
@validate_call
|
|
128
|
-
def remove_handler(
|
|
129
|
-
self, handler: Union[str, None] = None, handler_type: str = "NAME"
|
|
130
|
-
):
|
|
128
|
+
def remove_handler(self, handler: str | None = None, handler_type: str = "NAME"):
|
|
131
129
|
"""Remove all handlers or specific handler by name or id from logger.
|
|
132
130
|
|
|
133
131
|
Raises:
|
|
134
132
|
ValueError: The `handler_type` argument value '{handler_type}' is invalid, must be 'NAME' or 'ID'!
|
|
135
133
|
|
|
136
134
|
Args:
|
|
137
|
-
handler
|
|
138
|
-
|
|
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'.
|
|
139
137
|
"""
|
|
140
138
|
|
|
141
139
|
if handler:
|
|
@@ -162,7 +160,7 @@ class LoggerLoader:
|
|
|
162
160
|
self.handlers_map.clear()
|
|
163
161
|
|
|
164
162
|
@validate_call
|
|
165
|
-
def update_config(self, config:
|
|
163
|
+
def update_config(self, config: LoggerConfigPM | dict[str, Any]):
|
|
166
164
|
"""Update logger config with new config.
|
|
167
165
|
|
|
168
166
|
Args:
|
|
@@ -213,13 +211,11 @@ class LoggerLoader:
|
|
|
213
211
|
# elif self.config_file_path.lower().endswith(".toml"):
|
|
214
212
|
# _file_format = "TOML"
|
|
215
213
|
|
|
216
|
-
|
|
214
|
+
# Loading config from file, if it's exits:
|
|
217
215
|
if os.path.isfile(self.config_file_path):
|
|
218
216
|
if _file_format == "YAML":
|
|
219
217
|
try:
|
|
220
|
-
with open(
|
|
221
|
-
self.config_file_path, "r", encoding="utf-8"
|
|
222
|
-
) as _config_file:
|
|
218
|
+
with open(self.config_file_path, encoding="utf-8") as _config_file:
|
|
223
219
|
_new_config_dict = yaml.safe_load(_config_file) or {}
|
|
224
220
|
if "logger" not in _new_config_dict:
|
|
225
221
|
logger.warning(
|
|
@@ -242,9 +238,7 @@ class LoggerLoader:
|
|
|
242
238
|
raise
|
|
243
239
|
elif _file_format == "JSON":
|
|
244
240
|
try:
|
|
245
|
-
with open(
|
|
246
|
-
self.config_file_path, "r", encoding="utf-8"
|
|
247
|
-
) as _config_file:
|
|
241
|
+
with open(self.config_file_path, encoding="utf-8") as _config_file:
|
|
248
242
|
_new_config_dict = json.load(_config_file) or {}
|
|
249
243
|
if "logger" not in _new_config_dict:
|
|
250
244
|
logger.warning(
|
|
@@ -296,7 +290,7 @@ class LoggerLoader:
|
|
|
296
290
|
def _check_env(self):
|
|
297
291
|
"""Check environment variables for logger config."""
|
|
298
292
|
|
|
299
|
-
|
|
293
|
+
# Checking environment for DEBUG option:
|
|
300
294
|
_is_debug = False
|
|
301
295
|
_ENV = str(os.getenv("ENV")).strip().lower()
|
|
302
296
|
_DEBUG = str(os.getenv("DEBUG")).strip().lower()
|
|
@@ -314,7 +308,7 @@ class LoggerLoader:
|
|
|
314
308
|
self.config.file.logs_dir = os.getenv("BEANS_LOGGING_LOGS_DIR")
|
|
315
309
|
|
|
316
310
|
# if self.config.stream.use_color:
|
|
317
|
-
#
|
|
311
|
+
# # Checking terminal could support xterm colors:
|
|
318
312
|
# _TERM = str(os.getenv("TERM")).strip()
|
|
319
313
|
# if not "xterm" in _TERM:
|
|
320
314
|
# self.config.stream.use_color = False
|
|
@@ -536,7 +530,7 @@ class LoggerLoader:
|
|
|
536
530
|
|
|
537
531
|
_intercept_handler = InterceptHandler()
|
|
538
532
|
|
|
539
|
-
|
|
533
|
+
# Intercepting all logs from standard (root logger) logging:
|
|
540
534
|
logging.basicConfig(handlers=[_intercept_handler], level=0, force=True)
|
|
541
535
|
|
|
542
536
|
_intercepted_modules = set()
|
|
@@ -579,10 +573,10 @@ class LoggerLoader:
|
|
|
579
573
|
f"Intercepted modules: {list(_intercepted_modules)}; Muted modules: {list(_muted_modules)};"
|
|
580
574
|
)
|
|
581
575
|
|
|
582
|
-
|
|
583
|
-
|
|
576
|
+
# ATTRIBUTES #
|
|
577
|
+
# handlers_map
|
|
584
578
|
@property
|
|
585
|
-
def handlers_map(self) ->
|
|
579
|
+
def handlers_map(self) -> dict[str, int]:
|
|
586
580
|
try:
|
|
587
581
|
return self.__handlers_map
|
|
588
582
|
except AttributeError:
|
|
@@ -591,7 +585,7 @@ class LoggerLoader:
|
|
|
591
585
|
return self.__handlers_map
|
|
592
586
|
|
|
593
587
|
@handlers_map.setter
|
|
594
|
-
def handlers_map(self, handlers_map:
|
|
588
|
+
def handlers_map(self, handlers_map: dict[str, int]):
|
|
595
589
|
if not isinstance(handlers_map, dict):
|
|
596
590
|
raise TypeError(
|
|
597
591
|
f"`handlers_map` attribute type {type(handlers_map)} is invalid, must be <dict>!."
|
|
@@ -599,9 +593,9 @@ class LoggerLoader:
|
|
|
599
593
|
|
|
600
594
|
self.__handlers_map = copy.deepcopy(handlers_map)
|
|
601
595
|
|
|
602
|
-
|
|
596
|
+
# handlers_map
|
|
603
597
|
|
|
604
|
-
|
|
598
|
+
# config
|
|
605
599
|
@property
|
|
606
600
|
def config(self) -> LoggerConfigPM:
|
|
607
601
|
try:
|
|
@@ -620,9 +614,9 @@ class LoggerLoader:
|
|
|
620
614
|
|
|
621
615
|
self.__config = copy.deepcopy(config)
|
|
622
616
|
|
|
623
|
-
|
|
617
|
+
# config
|
|
624
618
|
|
|
625
|
-
|
|
619
|
+
# config_file_path
|
|
626
620
|
@property
|
|
627
621
|
def config_file_path(self) -> str:
|
|
628
622
|
try:
|
|
@@ -648,11 +642,13 @@ class LoggerLoader:
|
|
|
648
642
|
):
|
|
649
643
|
if not config_file_path.lower().endswith(".toml"):
|
|
650
644
|
raise NotImplementedError(
|
|
651
|
-
f"`config_file_path` attribute value '{config_file_path}' is invalid,
|
|
645
|
+
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
|
|
646
|
+
f"TOML file format is not supported yet!"
|
|
652
647
|
)
|
|
653
648
|
|
|
654
649
|
raise ValueError(
|
|
655
|
-
f"`config_file_path` attribute value '{config_file_path}' is invalid,
|
|
650
|
+
f"`config_file_path` attribute value '{config_file_path}' is invalid, "
|
|
651
|
+
f"file must be '.yml', '.yaml' or '.json' format!"
|
|
656
652
|
)
|
|
657
653
|
|
|
658
654
|
if not os.path.isabs(config_file_path):
|
|
@@ -660,5 +656,5 @@ class LoggerLoader:
|
|
|
660
656
|
|
|
661
657
|
self.__config_file_path = config_file_path
|
|
662
658
|
|
|
663
|
-
|
|
664
|
-
|
|
659
|
+
# config_file_path
|
|
660
|
+
# ATTRIBUTES #
|
beans_logging/_consts.py
CHANGED
beans_logging/_handlers.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
1
|
import sys
|
|
4
2
|
import logging
|
|
5
3
|
from logging import LogRecord
|
|
@@ -25,13 +23,13 @@ class InterceptHandler(logging.Handler):
|
|
|
25
23
|
record (LogRecord, required): Log needs to be handled.
|
|
26
24
|
"""
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
# Get corresponding Loguru level if it exists
|
|
29
27
|
try:
|
|
30
28
|
_level = logger.level(record.levelname).name
|
|
31
29
|
except ValueError:
|
|
32
30
|
_level = record.levelno
|
|
33
31
|
|
|
34
|
-
|
|
32
|
+
# Find caller from where originated the logged message
|
|
35
33
|
_frame, _depth = sys._getframe(6), 6
|
|
36
34
|
while _frame and _frame.f_code.co_filename == logging.__file__:
|
|
37
35
|
_frame = _frame.f_back
|
beans_logging/_utils.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
1
|
import os
|
|
4
2
|
import sys
|
|
5
3
|
import copy
|
|
@@ -22,7 +20,8 @@ def create_dir(create_dir: str, warn_mode: WarnEnum = WarnEnum.DEBUG):
|
|
|
22
20
|
|
|
23
21
|
Args:
|
|
24
22
|
create_dir (str, required): Create directory path.
|
|
25
|
-
warn_mode (str, optional): Warning message mode, for example: 'ERROR', 'ALWAYS', 'DEBUG', 'IGNORE'.
|
|
23
|
+
warn_mode (str, optional): Warning message mode, for example: 'ERROR', 'ALWAYS', 'DEBUG', 'IGNORE'.
|
|
24
|
+
Defaults to "DEBUG".
|
|
26
25
|
"""
|
|
27
26
|
|
|
28
27
|
if not os.path.isdir(create_dir):
|
beans_logging/auto.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# flake8: noqa
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
|
-
from typing import Union
|
|
5
4
|
|
|
6
5
|
from . import *
|
|
7
6
|
|
|
8
|
-
logger_loader:
|
|
7
|
+
logger_loader: LoggerLoader | None = None
|
|
9
8
|
_DISABLE_DEFAULT_LOGGER = (
|
|
10
9
|
str(os.getenv("BEANS_LOGGING_DISABLE_DEFAULT")).strip().lower()
|
|
11
10
|
)
|
beans_logging/filters.py
CHANGED
beans_logging/formats.py
CHANGED
beans_logging/rotation.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
1
|
import datetime
|
|
4
2
|
from typing import TextIO
|
|
5
3
|
|
|
@@ -35,8 +33,8 @@ class RotationChecker:
|
|
|
35
33
|
)
|
|
36
34
|
|
|
37
35
|
if _current_dtime >= self._dtime_limit:
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
# The current time is already past the target time so it would rotate already.
|
|
37
|
+
# Add one day to prevent an immediate rotation.
|
|
40
38
|
self._dtime_limit += datetime.timedelta(days=1)
|
|
41
39
|
|
|
42
40
|
def should_rotate(self, message: Message, file: TextIO) -> bool:
|
beans_logging/schemas.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
1
|
import datetime
|
|
4
|
-
from typing import List
|
|
5
2
|
|
|
6
3
|
|
|
7
4
|
import pydantic
|
|
@@ -34,7 +31,10 @@ class StreamPM(ExtraBaseModel):
|
|
|
34
31
|
use_color: bool = Field(default=True)
|
|
35
32
|
use_icon: bool = Field(default=False)
|
|
36
33
|
format_str: constr(strip_whitespace=True) = Field(
|
|
37
|
-
default=
|
|
34
|
+
default=(
|
|
35
|
+
"[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: "
|
|
36
|
+
"<level>{message}</level>"
|
|
37
|
+
),
|
|
38
38
|
min_length=3,
|
|
39
39
|
max_length=511,
|
|
40
40
|
)
|
|
@@ -145,13 +145,13 @@ class FilePM(ExtraBaseModel):
|
|
|
145
145
|
class AutoLoadPM(ExtraBaseModel):
|
|
146
146
|
enabled: bool = Field(default=True)
|
|
147
147
|
only_base: bool = Field(default=False)
|
|
148
|
-
ignore_modules:
|
|
148
|
+
ignore_modules: list[str] = Field(default=[])
|
|
149
149
|
|
|
150
150
|
|
|
151
151
|
class InterceptPM(ExtraBaseModel):
|
|
152
152
|
auto_load: AutoLoadPM = Field(default_factory=AutoLoadPM)
|
|
153
|
-
include_modules:
|
|
154
|
-
mute_modules:
|
|
153
|
+
include_modules: list[str] = Field(default=[])
|
|
154
|
+
mute_modules: list[str] = Field(default=[])
|
|
155
155
|
|
|
156
156
|
|
|
157
157
|
class ExtraPM(ExtraBaseModel):
|
beans_logging/sinks.py
CHANGED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: beans_logging
|
|
3
|
+
Version: 6.0.2
|
|
4
|
+
Summary: 'beans-logging' is a python package for simple logger and easily managing logs.
|
|
5
|
+
Author-email: Batkhuu Byambajav <batkhuu10@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/bybatkhuu/module-python-logging
|
|
7
|
+
Project-URL: Documentation, https://pylogging-docs.bybatkhuu.dev
|
|
8
|
+
Project-URL: Repository, https://github.com/bybatkhuu/module-python-logging.git
|
|
9
|
+
Project-URL: Issues, https://github.com/bybatkhuu/module-python-logging/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/bybatkhuu/module-python-logging/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: beans_logging,loguru,logging,logger,logs,python-logging,custom-logging
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Requires-Python: <4.0,>=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE.txt
|
|
24
|
+
Requires-Dist: PyYAML<7.0,>=6.0.2
|
|
25
|
+
Requires-Dist: pydantic[timezone]!=2.1.0,<3.0.0,>=2.0.3
|
|
26
|
+
Requires-Dist: loguru<1.0.0,>=0.7.3
|
|
27
|
+
Provides-Extra: fastapi
|
|
28
|
+
Requires-Dist: beans-logging-fastapi<2.0.0,>=1.0.0; extra == "fastapi"
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest<9.0.0,>=8.0.2; extra == "test"
|
|
31
|
+
Requires-Dist: pytest-cov<8.0.0,>=5.0.0; extra == "test"
|
|
32
|
+
Requires-Dist: pytest-xdist<4.0.0,>=3.6.1; extra == "test"
|
|
33
|
+
Requires-Dist: pytest-benchmark<6.0.0,>=5.0.1; extra == "test"
|
|
34
|
+
Provides-Extra: build
|
|
35
|
+
Requires-Dist: setuptools<81.0.0,>=70.3.0; extra == "build"
|
|
36
|
+
Requires-Dist: wheel<1.0.0,>=0.43.0; extra == "build"
|
|
37
|
+
Requires-Dist: build<2.0.0,>=1.1.1; extra == "build"
|
|
38
|
+
Requires-Dist: twine<7.0.0,>=6.0.1; extra == "build"
|
|
39
|
+
Provides-Extra: docs
|
|
40
|
+
Requires-Dist: pylint<4.0.0,>=3.0.4; extra == "docs"
|
|
41
|
+
Requires-Dist: mkdocs-material<10.0.0,>=9.5.50; extra == "docs"
|
|
42
|
+
Requires-Dist: mkdocs-awesome-nav<4.0.0,>=3.0.0; extra == "docs"
|
|
43
|
+
Requires-Dist: mkdocstrings[python]<1.0.0,>=0.24.3; extra == "docs"
|
|
44
|
+
Requires-Dist: mike<3.0.0,>=2.1.3; extra == "docs"
|
|
45
|
+
Provides-Extra: dev
|
|
46
|
+
Requires-Dist: pytest<9.0.0,>=8.0.2; extra == "dev"
|
|
47
|
+
Requires-Dist: pytest-cov<8.0.0,>=5.0.0; extra == "dev"
|
|
48
|
+
Requires-Dist: pytest-xdist<4.0.0,>=3.6.1; extra == "dev"
|
|
49
|
+
Requires-Dist: pytest-benchmark<6.0.0,>=5.0.1; extra == "dev"
|
|
50
|
+
Requires-Dist: setuptools<81.0.0,>=70.3.0; extra == "dev"
|
|
51
|
+
Requires-Dist: wheel<1.0.0,>=0.43.0; extra == "dev"
|
|
52
|
+
Requires-Dist: build<2.0.0,>=1.1.1; extra == "dev"
|
|
53
|
+
Requires-Dist: twine<7.0.0,>=6.0.1; extra == "dev"
|
|
54
|
+
Requires-Dist: pylint<4.0.0,>=3.0.4; extra == "dev"
|
|
55
|
+
Requires-Dist: mkdocs-material<10.0.0,>=9.5.50; extra == "dev"
|
|
56
|
+
Requires-Dist: mkdocs-awesome-nav<4.0.0,>=3.0.0; extra == "dev"
|
|
57
|
+
Requires-Dist: mkdocstrings[python]<1.0.0,>=0.24.3; extra == "dev"
|
|
58
|
+
Requires-Dist: mike<3.0.0,>=2.1.3; extra == "dev"
|
|
59
|
+
Requires-Dist: pyright<2.0.0,>=1.1.392; extra == "dev"
|
|
60
|
+
Requires-Dist: pre-commit<5.0.0,>=4.0.1; extra == "dev"
|
|
61
|
+
Dynamic: license-file
|
|
62
|
+
|
|
63
|
+
# Python Logging (beans-logging)
|
|
64
|
+
|
|
65
|
+
[](https://choosealicense.com/licenses/mit)
|
|
66
|
+
[](https://github.com/bybatkhuu/module-python-logging/actions/workflows/2.build-publish.yml)
|
|
67
|
+
[](https://github.com/bybatkhuu/module-python-logging/releases)
|
|
68
|
+
[](https://pypi.org/project/beans-logging)
|
|
69
|
+
[](https://docs.conda.io/en/latest/miniconda.html)
|
|
70
|
+
|
|
71
|
+
`beans-logging` is a python package for simple logger and easily managing logs.
|
|
72
|
+
|
|
73
|
+
It is a `Loguru` based custom logging package for python projects.
|
|
74
|
+
|
|
75
|
+
## ✨ Features
|
|
76
|
+
|
|
77
|
+
- Main **logger** based on **Loguru** logging - <https://pypi.org/project/loguru>
|
|
78
|
+
- Logging to **log files** (all, error, json)
|
|
79
|
+
- **Pre-defined** logging configs and handlers
|
|
80
|
+
- **Colorful** logging
|
|
81
|
+
- Auto **intercepting** and **muting** modules
|
|
82
|
+
- Load config from **YAML** or **JSON** file
|
|
83
|
+
- Custom options as a **config**
|
|
84
|
+
- Custom logging **formats**
|
|
85
|
+
- **Multiprocess** compatibility (Linux, macOS - 'fork')
|
|
86
|
+
- Add custom **handlers**
|
|
87
|
+
- **Base** logging module
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## 🛠 Installation
|
|
92
|
+
|
|
93
|
+
### 1. 🚧 Prerequisites
|
|
94
|
+
|
|
95
|
+
- Install **Python (>= v3.10)** and **pip (>= 23)**:
|
|
96
|
+
- **[RECOMMENDED] [Miniconda (v3)](https://www.anaconda.com/docs/getting-started/miniconda/install)**
|
|
97
|
+
- *[arm64/aarch64] [Miniforge (v3)](https://github.com/conda-forge/miniforge)*
|
|
98
|
+
- *[Python virutal environment] [venv](https://docs.python.org/3/library/venv.html)*
|
|
99
|
+
|
|
100
|
+
[OPTIONAL] For **DEVELOPMENT** environment:
|
|
101
|
+
|
|
102
|
+
- Install [**git**](https://git-scm.com/downloads)
|
|
103
|
+
- Setup an [**SSH key**](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh)
|
|
104
|
+
|
|
105
|
+
### 2. 📥 Download or clone the repository
|
|
106
|
+
|
|
107
|
+
[TIP] Skip this step, if you're going to install the package directly from **PyPi** or **GitHub** repository.
|
|
108
|
+
|
|
109
|
+
**2.1.** Prepare projects directory (if not exists):
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
# Create projects directory:
|
|
113
|
+
mkdir -pv ~/workspaces/projects
|
|
114
|
+
|
|
115
|
+
# Enter into projects directory:
|
|
116
|
+
cd ~/workspaces/projects
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**2.2.** Follow one of the below options **[A]**, **[B]** or **[C]**:
|
|
120
|
+
|
|
121
|
+
**OPTION A.** Clone the repository:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
git clone https://github.com/bybatkhuu/module-python-logging.git && \
|
|
125
|
+
cd module-python-logging
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**OPTION B.** Clone the repository (for **DEVELOPMENT**: git + ssh key):
|
|
129
|
+
|
|
130
|
+
```sh
|
|
131
|
+
git clone git@github.com:bybatkhuu/module-python-logging.git && \
|
|
132
|
+
cd module-python-logging
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**OPTION C.** Download source code:
|
|
136
|
+
|
|
137
|
+
1. Download archived **zip** file from [**releases**](https://github.com/bybatkhuu/module-python-logging/releases).
|
|
138
|
+
2. Extract it into the projects directory.
|
|
139
|
+
|
|
140
|
+
### 3. 📦 Install the package
|
|
141
|
+
|
|
142
|
+
[NOTE] Choose one of the following methods to install the package **[A ~ F]**:
|
|
143
|
+
|
|
144
|
+
**OPTION A.** [**RECOMMENDED**] Install from **PyPi**:
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
pip install -U beans-logging
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**OPTION B.** Install latest version directly from **GitHub** repository:
|
|
151
|
+
|
|
152
|
+
```sh
|
|
153
|
+
pip install git+https://github.com/bybatkhuu/module-python-logging.git
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**OPTION C.** Install from the downloaded **source code**:
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
# Install directly from the source code:
|
|
160
|
+
pip install .
|
|
161
|
+
|
|
162
|
+
# Or install with editable mode:
|
|
163
|
+
pip install -e .
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**OPTION D.** Install for **DEVELOPMENT** environment:
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
pip install -e .[dev]
|
|
170
|
+
|
|
171
|
+
# Install pre-commit hooks:
|
|
172
|
+
pre-commit install
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**OPTION E.** Install from **pre-built release** files:
|
|
176
|
+
|
|
177
|
+
1. Download **`.whl`** or **`.tar.gz`** file from [**releases**](https://github.com/bybatkhuu/module-python-logging/releases)
|
|
178
|
+
2. Install with pip:
|
|
179
|
+
|
|
180
|
+
```sh
|
|
181
|
+
# Install from .whl file:
|
|
182
|
+
pip install ./beans_logging-[VERSION]-py3-none-any.whl
|
|
183
|
+
|
|
184
|
+
# Or install from .tar.gz file:
|
|
185
|
+
pip install ./beans_logging-[VERSION].tar.gz
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**OPTION F.** Copy the **module** into the project directory (for **testing**):
|
|
189
|
+
|
|
190
|
+
```sh
|
|
191
|
+
# Install python dependencies:
|
|
192
|
+
pip install -r ./requirements.txt
|
|
193
|
+
|
|
194
|
+
# Copy the module source code into the project:
|
|
195
|
+
cp -r ./src/beans_logging [PROJECT_DIR]
|
|
196
|
+
# For example:
|
|
197
|
+
cp -r ./src/beans_logging /some/path/project/
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## 🚸 Usage/Examples
|
|
201
|
+
|
|
202
|
+
To use `beans_logging`, import the `logger` instance from the `beans_logging.auto` package:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from beans_logging.auto import logger
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
You can call logging methods directly from the `logger` instance:
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
logger.info("Logging info.")
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### **Simple**
|
|
215
|
+
|
|
216
|
+
[**`configs/logger.yml`**](./examples/simple/configs/logger.yml):
|
|
217
|
+
|
|
218
|
+
```yml
|
|
219
|
+
logger:
|
|
220
|
+
app_name: "my-app"
|
|
221
|
+
level: "TRACE"
|
|
222
|
+
file:
|
|
223
|
+
log_handlers:
|
|
224
|
+
enabled: true
|
|
225
|
+
json_handlers:
|
|
226
|
+
enabled: true
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
[**`main.py`**](./examples/simple/main.py):
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
#!/usr/bin/env python
|
|
233
|
+
|
|
234
|
+
from beans_logging.auto import logger
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
logger.trace("Tracing...")
|
|
238
|
+
logger.debug("Debugging...")
|
|
239
|
+
logger.info("Logging info.")
|
|
240
|
+
logger.success("Success.")
|
|
241
|
+
logger.warning("Warning something.")
|
|
242
|
+
logger.error("Error occured.")
|
|
243
|
+
logger.critical("CRITICAL ERROR.")
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def divide(a, b):
|
|
247
|
+
_result = a / b
|
|
248
|
+
return _result
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def nested(c):
|
|
252
|
+
try:
|
|
253
|
+
divide(5, c)
|
|
254
|
+
except ZeroDivisionError as err:
|
|
255
|
+
logger.error(err)
|
|
256
|
+
raise
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
try:
|
|
260
|
+
nested(0)
|
|
261
|
+
except Exception:
|
|
262
|
+
logger.exception("Show me, what value is wrong:")
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Run the [**`examples/simple`**](./examples/simple):
|
|
266
|
+
|
|
267
|
+
```sh
|
|
268
|
+
cd ./examples/simple
|
|
269
|
+
|
|
270
|
+
python ./main.py
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**Output**:
|
|
274
|
+
|
|
275
|
+
```txt
|
|
276
|
+
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
|
|
277
|
+
[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...
|
|
278
|
+
[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...
|
|
279
|
+
[2023-09-01 00:00:00.000 +09:00 | INFO | __main__:9]: Logging info.
|
|
280
|
+
[2023-09-01 00:00:00.000 +09:00 | OK | __main__:10]: Success.
|
|
281
|
+
[2023-09-01 00:00:00.000 +09:00 | WARN | __main__:11]: Warning something.
|
|
282
|
+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.
|
|
283
|
+
[2023-09-01 00:00:00.000 +09:00 | CRIT | __main__:13]: CRITICAL ERROR.
|
|
284
|
+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero
|
|
285
|
+
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
|
|
286
|
+
Traceback (most recent call last):
|
|
287
|
+
|
|
288
|
+
> File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 30, in <module>
|
|
289
|
+
nested(0)
|
|
290
|
+
└ <function nested at 0x10802a4c0>
|
|
291
|
+
|
|
292
|
+
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 23, in nested
|
|
293
|
+
divide(5, c)
|
|
294
|
+
│ └ 0
|
|
295
|
+
└ <function divide at 0x1052f31f0>
|
|
296
|
+
|
|
297
|
+
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 17, in divide
|
|
298
|
+
_result = a / b
|
|
299
|
+
│ └ 0
|
|
300
|
+
└ 5
|
|
301
|
+
|
|
302
|
+
ZeroDivisionError: division by zero
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
👍
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## ⚙️ Configuration
|
|
310
|
+
|
|
311
|
+
[**`templates/configs/logger.yml`**](./templates/configs/logger.yml):
|
|
312
|
+
|
|
313
|
+
```yaml
|
|
314
|
+
logger:
|
|
315
|
+
# app_name: "app"
|
|
316
|
+
level: "INFO"
|
|
317
|
+
use_diagnose: false
|
|
318
|
+
stream:
|
|
319
|
+
use_color: true
|
|
320
|
+
use_icon: false
|
|
321
|
+
format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
|
|
322
|
+
std_handler:
|
|
323
|
+
enabled: true
|
|
324
|
+
file:
|
|
325
|
+
logs_dir: "./logs"
|
|
326
|
+
rotate_size: 10000000 # 10MB
|
|
327
|
+
rotate_time: "00:00:00"
|
|
328
|
+
backup_count: 90
|
|
329
|
+
log_handlers:
|
|
330
|
+
enabled: false
|
|
331
|
+
format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
|
|
332
|
+
log_path: "{app_name}.std.all.log"
|
|
333
|
+
err_path: "{app_name}.std.err.log"
|
|
334
|
+
json_handlers:
|
|
335
|
+
enabled: false
|
|
336
|
+
use_custom: false
|
|
337
|
+
log_path: "{app_name}.json.all.log"
|
|
338
|
+
err_path: "{app_name}.json.err.log"
|
|
339
|
+
intercept:
|
|
340
|
+
auto_load:
|
|
341
|
+
enabled: true
|
|
342
|
+
only_base: false
|
|
343
|
+
ignore_modules: []
|
|
344
|
+
include_modules: []
|
|
345
|
+
mute_modules: []
|
|
346
|
+
extra:
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### 🌎 Environment Variables
|
|
350
|
+
|
|
351
|
+
[**`.env.example`**](./.env.example):
|
|
352
|
+
|
|
353
|
+
```sh
|
|
354
|
+
# ENV=LOCAL
|
|
355
|
+
# DEBUG=false
|
|
356
|
+
# TZ=UTC
|
|
357
|
+
|
|
358
|
+
# BEANS_LOGGING_DISABLE_DEFAULT=false
|
|
359
|
+
# BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
|
|
360
|
+
# BEANS_LOGGING_LOGS_DIR="./logs"
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
---
|
|
364
|
+
|
|
365
|
+
## 🧪 Running Tests
|
|
366
|
+
|
|
367
|
+
To run tests, run the following command:
|
|
368
|
+
|
|
369
|
+
```sh
|
|
370
|
+
# Install python test dependencies:
|
|
371
|
+
pip install .[test]
|
|
372
|
+
|
|
373
|
+
# Run tests:
|
|
374
|
+
python -m pytest -sv -o log_cli=true
|
|
375
|
+
# Or use the test script:
|
|
376
|
+
./scripts/test.sh -l -v -c
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## 🏗️ Build Package
|
|
380
|
+
|
|
381
|
+
To build the python package, run the following command:
|
|
382
|
+
|
|
383
|
+
```sh
|
|
384
|
+
# Install python build dependencies:
|
|
385
|
+
pip install -r ./requirements/requirements.build.txt
|
|
386
|
+
|
|
387
|
+
# Build python package:
|
|
388
|
+
python -m build
|
|
389
|
+
# Or use the build script:
|
|
390
|
+
./scripts/build.sh
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
## 📝 Generate Docs
|
|
394
|
+
|
|
395
|
+
To build the documentation, run the following command:
|
|
396
|
+
|
|
397
|
+
```sh
|
|
398
|
+
# Install python documentation dependencies:
|
|
399
|
+
pip install -r ./requirements/requirements.docs.txt
|
|
400
|
+
|
|
401
|
+
# Serve documentation locally (for development):
|
|
402
|
+
mkdocs serve -a 0.0.0.0:8000
|
|
403
|
+
# Or use the docs script:
|
|
404
|
+
./scripts/docs.sh
|
|
405
|
+
|
|
406
|
+
# Or build documentation:
|
|
407
|
+
mkdocs build
|
|
408
|
+
# Or use the docs script:
|
|
409
|
+
./scripts/docs.sh -b
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
## 📚 Documentation
|
|
413
|
+
|
|
414
|
+
- [Docs](./docs)
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
## 📑 References
|
|
419
|
+
|
|
420
|
+
- <https://github.com/Delgan/loguru>
|
|
421
|
+
- <https://loguru.readthedocs.io/en/stable/api/logger.html>
|
|
422
|
+
- <https://loguru.readthedocs.io/en/stable/resources/recipes.html>
|
|
423
|
+
- <https://docs.python.org/3/library/logging.html>
|
|
424
|
+
- <https://github.com/bybatkhuu/module-fastapi-logging>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
beans_logging/__init__.py,sha256=Ax1f3I26MSEorCBnkPVcuzEWN7IoJZuzPdVjCukvUv0,272
|
|
2
|
+
beans_logging/__version__.py,sha256=lze97Y7ToUnpENKGAhOoSKx1zlZQe6ZfCSr8M50_K1g,22
|
|
3
|
+
beans_logging/_base.py,sha256=fKwGgtkbUzg2Wtfb5XwIGF5UU56iPOT-R6OD3wqIBws,24645
|
|
4
|
+
beans_logging/_consts.py,sha256=OqSvH2IvsUSbvIgthHeiv92i9CHXKYvO-PCb_DsMANI,320
|
|
5
|
+
beans_logging/_handlers.py,sha256=cz2jcJR0MuVHWWNRAFT617njC_18Q56lNcgKIxLmFys,1106
|
|
6
|
+
beans_logging/_utils.py,sha256=Vk8UT2mPTanP4KuLnxKk6Pv-Nr8iWDom0TwCxHb5EhA,2757
|
|
7
|
+
beans_logging/auto.py,sha256=YQMfmWK6mFEwqabhimj8ZA_yStKZCDQEvteKv2UIbEA,494
|
|
8
|
+
beans_logging/filters.py,sha256=iikVBMIhAF_DP1F0X0GJ7GUOvP-JdcH_CxAsYuz0UxM,3343
|
|
9
|
+
beans_logging/formats.py,sha256=WTIDP0uJL9J9bcFZ2_Qe8zlw_l4SrE2k2agPLvrjfMM,1324
|
|
10
|
+
beans_logging/rotation.py,sha256=t-ORXq7x4tDbvz8XMjCCuDZ6cUApcu3SBQzfgTwHQU8,2110
|
|
11
|
+
beans_logging/schemas.py,sha256=CBaztXAnoqKiL3poLz0ymR8c0EdqKo4rJ_K4kXwDqjo,5631
|
|
12
|
+
beans_logging/sinks.py,sha256=t2U4v95H_tV2ci6YS8aKCnTUKVRxEcsLnB55qn4wyrQ,331
|
|
13
|
+
beans_logging-6.0.2.dist-info/licenses/LICENSE.txt,sha256=CUTK-r0BWIg1r0bBiemAcMhakgV0N7HuRhw6rQ-A9A4,1074
|
|
14
|
+
beans_logging-6.0.2.dist-info/METADATA,sha256=E56H2f4nvUltwTr-k3ZfTrTsdeleUpGcb0FmtvaDInk,12652
|
|
15
|
+
beans_logging-6.0.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
16
|
+
beans_logging-6.0.2.dist-info/top_level.txt,sha256=lx8JEqYGNha1sYbVrTtMo2Z01A7Shq8hX6bfsuKLTG8,14
|
|
17
|
+
beans_logging-6.0.2.dist-info/RECORD,,
|
|
@@ -1,326 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: beans-logging
|
|
3
|
-
Version: 6.0.0
|
|
4
|
-
Summary: 'beans_logging' is a python package for simple logger and easily managing logging modules. It is a Loguru based custom logging package for python projects.
|
|
5
|
-
Home-page: https://github.com/bybatkhuu/module.python-logging
|
|
6
|
-
Download-URL: https://github.com/bybatkhuu/module.python-logging/archive/v6.0.0.tar.gz
|
|
7
|
-
Author: Batkhuu Byambajav
|
|
8
|
-
Author-email: batkhuu10@gmail.com
|
|
9
|
-
License: MIT
|
|
10
|
-
Keywords: beans_logging,loguru,logging,logger,logs,log,print,custom-logging
|
|
11
|
-
Classifier: Development Status :: 4 - Beta
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: Topic :: Software Development :: Build Tools
|
|
14
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
-
Requires-Python: >=3.8
|
|
20
|
-
Description-Content-Type: text/markdown
|
|
21
|
-
License-File: LICENSE.txt
|
|
22
|
-
Requires-Dist: PyYAML <7.0,>=6.0
|
|
23
|
-
Requires-Dist: pydantic !=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.10.0
|
|
24
|
-
Requires-Dist: loguru <1.0.0,>=0.7.2
|
|
25
|
-
Provides-Extra: fastapi
|
|
26
|
-
Requires-Dist: beans-logging-fastapi <2.0.0,>=1.0.0 ; extra == 'fastapi'
|
|
27
|
-
|
|
28
|
-
# beans_logging
|
|
29
|
-
|
|
30
|
-
[](https://choosealicense.com/licenses/mit/)
|
|
31
|
-
[](https://github.com/bybatkhuu/module.python-logging/actions/workflows/2.build-publish.yml)
|
|
32
|
-
[](https://github.com/bybatkhuu/module.python-logging/releases)
|
|
33
|
-
[](https://pypi.org/project/beans-logging)
|
|
34
|
-
[](https://docs.conda.io/en/latest/miniconda.html)
|
|
35
|
-
|
|
36
|
-
`beans_logging` is a python package for simple logger and easily managing logging modules.
|
|
37
|
-
|
|
38
|
-
It is a `Loguru` based custom logging package for python projects.
|
|
39
|
-
|
|
40
|
-
## Features
|
|
41
|
-
|
|
42
|
-
- Main **logger** based on **Loguru** logging - <https://pypi.org/project/loguru>
|
|
43
|
-
- Logging to **log files** (all, error, json)
|
|
44
|
-
- **Pre-defined** logging configs and handlers
|
|
45
|
-
- **Colorful** logging
|
|
46
|
-
- Auto **intercepting** and **muting** modules
|
|
47
|
-
- Load config from **YAML** or **JSON** file
|
|
48
|
-
- Custom options as a **config**
|
|
49
|
-
- Custom logging **formats**
|
|
50
|
-
- **Multiprocess** compatibility (Linux, macOS - 'fork')
|
|
51
|
-
- Add custom **handlers**
|
|
52
|
-
- **Base** logging module
|
|
53
|
-
- Support **Pydantic-v1** and **Pydantic-v2**
|
|
54
|
-
|
|
55
|
-
---
|
|
56
|
-
|
|
57
|
-
## Installation
|
|
58
|
-
|
|
59
|
-
### 1. Prerequisites
|
|
60
|
-
|
|
61
|
-
- **Python (>= v3.8)**
|
|
62
|
-
- **PyPi (>= v23)**
|
|
63
|
-
|
|
64
|
-
### 2. Install beans-logging package
|
|
65
|
-
|
|
66
|
-
Choose one of the following methods to install the package **[A ~ F]**:
|
|
67
|
-
|
|
68
|
-
**A.** [**RECOMMENDED**] Install from **PyPi**
|
|
69
|
-
|
|
70
|
-
```sh
|
|
71
|
-
# Install or upgrade beans-logging package:
|
|
72
|
-
pip install -U beans-logging
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
**B.** Install latest version from **GitHub**
|
|
76
|
-
|
|
77
|
-
```sh
|
|
78
|
-
# Install package by git:
|
|
79
|
-
pip install git+https://github.com/bybatkhuu/module.python-logging.git
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
**C.** Install from **pre-built release** files
|
|
83
|
-
|
|
84
|
-
1. Download **`.whl`** or **`.tar.gz`** file from **releases** - <https://github.com/bybatkhuu/module.python-logging/releases>
|
|
85
|
-
2. Install with pip:
|
|
86
|
-
|
|
87
|
-
```sh
|
|
88
|
-
# Install from .whl file:
|
|
89
|
-
pip install ./beans_logging-[VERSION]-py3-none-any.whl
|
|
90
|
-
# Or install from .tar.gz file:
|
|
91
|
-
pip install ./beans_logging-[VERSION].tar.gz
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
**D.** Install from **source code** by building package
|
|
95
|
-
|
|
96
|
-
```sh
|
|
97
|
-
# Clone repository by git:
|
|
98
|
-
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
|
|
99
|
-
cd ./beans_logging
|
|
100
|
-
|
|
101
|
-
# Install python build tool:
|
|
102
|
-
pip install -U pip build
|
|
103
|
-
|
|
104
|
-
# Build python package:
|
|
105
|
-
python -m build
|
|
106
|
-
|
|
107
|
-
_VERSION=$(./scripts/get-version.sh)
|
|
108
|
-
|
|
109
|
-
# Install from .whl file:
|
|
110
|
-
pip install ./dist/beans_logging-${_VERSION}-py3-none-any.whl
|
|
111
|
-
# Or install from .tar.gz file:
|
|
112
|
-
pip install ./dist/beans_logging-${_VERSION}.tar.gz
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
**E.** Install with pip editable **development mode** (from source code)
|
|
116
|
-
|
|
117
|
-
```sh
|
|
118
|
-
# Clone repository by git:
|
|
119
|
-
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
|
|
120
|
-
cd ./beans_logging
|
|
121
|
-
|
|
122
|
-
# Install with editable development mode:
|
|
123
|
-
pip install -e .
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
**F.** Manually add to **PYTHONPATH** (not recommended)
|
|
127
|
-
|
|
128
|
-
```sh
|
|
129
|
-
# Clone repository by git:
|
|
130
|
-
git clone https://github.com/bybatkhuu/module.python-logging.git beans_logging
|
|
131
|
-
cd ./beans_logging
|
|
132
|
-
|
|
133
|
-
# Install python dependencies:
|
|
134
|
-
pip install -r ./requirements.txt
|
|
135
|
-
|
|
136
|
-
# Add current path to PYTHONPATH:
|
|
137
|
-
export PYTHONPATH="${PWD}:${PYTHONPATH}"
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## Usage/Examples
|
|
141
|
-
|
|
142
|
-
To use `beans_logging`, import the `logger` instance from the `beans_logging.auto` package:
|
|
143
|
-
|
|
144
|
-
```python
|
|
145
|
-
from beans_logging.auto import logger
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
You can call logging methods directly from the `logger` instance:
|
|
149
|
-
|
|
150
|
-
```python
|
|
151
|
-
logger.info("Logging info.")
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### **Simple**
|
|
155
|
-
|
|
156
|
-
[**`configs/logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/configs/logger.yml):
|
|
157
|
-
|
|
158
|
-
```yml
|
|
159
|
-
logger:
|
|
160
|
-
app_name: "my-app"
|
|
161
|
-
level: "TRACE"
|
|
162
|
-
file:
|
|
163
|
-
log_handlers:
|
|
164
|
-
enabled: true
|
|
165
|
-
json_handlers:
|
|
166
|
-
enabled: true
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
[**`main.py`**](https://github.com/bybatkhuu/module.python-logging/blob/main/examples/simple/main.py):
|
|
170
|
-
|
|
171
|
-
```python
|
|
172
|
-
from beans_logging.auto import logger
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
logger.trace("Tracing...")
|
|
176
|
-
logger.debug("Debugging...")
|
|
177
|
-
logger.info("Logging info.")
|
|
178
|
-
logger.success("Success.")
|
|
179
|
-
logger.warning("Warning something.")
|
|
180
|
-
logger.error("Error occured.")
|
|
181
|
-
logger.critical("CRITICAL ERROR.")
|
|
182
|
-
|
|
183
|
-
def divide(a, b):
|
|
184
|
-
_result = a / b
|
|
185
|
-
return _result
|
|
186
|
-
|
|
187
|
-
def nested(c):
|
|
188
|
-
try:
|
|
189
|
-
divide(5, c)
|
|
190
|
-
except ZeroDivisionError as err:
|
|
191
|
-
logger.error(err)
|
|
192
|
-
raise
|
|
193
|
-
|
|
194
|
-
try:
|
|
195
|
-
nested(0)
|
|
196
|
-
except Exception as err:
|
|
197
|
-
logger.exception("Show me, what value is wrong:")
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
Run the [**`examples/simple`**](https://github.com/bybatkhuu/module.python-logging/tree/main/examples/simple):
|
|
201
|
-
|
|
202
|
-
```sh
|
|
203
|
-
cd ./examples/simple
|
|
204
|
-
|
|
205
|
-
python ./main.py
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
**Output**:
|
|
209
|
-
|
|
210
|
-
```txt
|
|
211
|
-
[2023-09-01 00:00:00.000 +09:00 | TRACE | beans_logging._base:478]: Intercepted modules: ['concurrent', 'concurrent.futures', 'asyncio']; Muted modules: [];
|
|
212
|
-
[2023-09-01 00:00:00.000 +09:00 | TRACE | __main__:7]: Tracing...
|
|
213
|
-
[2023-09-01 00:00:00.000 +09:00 | DEBUG | __main__:8]: Debugging...
|
|
214
|
-
[2023-09-01 00:00:00.000 +09:00 | INFO | __main__:9]: Logging info.
|
|
215
|
-
[2023-09-01 00:00:00.000 +09:00 | OK | __main__:10]: Success.
|
|
216
|
-
[2023-09-01 00:00:00.000 +09:00 | WARN | __main__:11]: Warning something.
|
|
217
|
-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:12]: Error occured.
|
|
218
|
-
[2023-09-01 00:00:00.000 +09:00 | CRIT | __main__:13]: CRITICAL ERROR.
|
|
219
|
-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:25]: division by zero
|
|
220
|
-
[2023-09-01 00:00:00.000 +09:00 | ERROR | __main__:32]: Show me, what value is wrong:
|
|
221
|
-
Traceback (most recent call last):
|
|
222
|
-
|
|
223
|
-
> File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 30, in <module>
|
|
224
|
-
nested(0)
|
|
225
|
-
└ <function nested at 0x10802a4c0>
|
|
226
|
-
|
|
227
|
-
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 23, in nested
|
|
228
|
-
divide(5, c)
|
|
229
|
-
│ └ 0
|
|
230
|
-
└ <function divide at 0x1052f31f0>
|
|
231
|
-
|
|
232
|
-
File "/home/user/workspaces/projects/beans_logging/examples/simple/./main.py", line 17, in divide
|
|
233
|
-
_result = a / b
|
|
234
|
-
│ └ 0
|
|
235
|
-
└ 5
|
|
236
|
-
|
|
237
|
-
ZeroDivisionError: division by zero
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
### **FastAPI**
|
|
241
|
-
|
|
242
|
-
Checkout `beans_logging_fastapi` package: <https://github.com/bybatkhuu/module.fastapi-logging>
|
|
243
|
-
|
|
244
|
-
- FastAPI HTTP access logging middleware
|
|
245
|
-
- Install with pip: `pip install -U beans-logging[fastapi]` or `pip install -U beans-logging-fastapi`
|
|
246
|
-
|
|
247
|
-
---
|
|
248
|
-
|
|
249
|
-
## Running Tests
|
|
250
|
-
|
|
251
|
-
To run tests, run the following command:
|
|
252
|
-
|
|
253
|
-
```sh
|
|
254
|
-
# Install python test dependencies:
|
|
255
|
-
pip install -r ./requirements.test.txt
|
|
256
|
-
|
|
257
|
-
# Run tests:
|
|
258
|
-
python -m pytest -v
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
## Environment Variables
|
|
262
|
-
|
|
263
|
-
You can use the following environment variables inside [**`.env.example`**](https://github.com/bybatkhuu/module.python-logging/blob/main/.env.example) file:
|
|
264
|
-
|
|
265
|
-
```sh
|
|
266
|
-
ENV=development
|
|
267
|
-
DEBUG=true
|
|
268
|
-
|
|
269
|
-
BEANS_LOGGING_DISABLE_DEFAULT=false
|
|
270
|
-
BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
|
|
271
|
-
BEANS_LOGGING_LOGS_DIR="./logs"
|
|
272
|
-
```
|
|
273
|
-
|
|
274
|
-
## Configuration
|
|
275
|
-
|
|
276
|
-
You can use the following configuration template [**`logger.yml`**](https://github.com/bybatkhuu/module.python-logging/blob/main/templates/configs/logger.yml): file:
|
|
277
|
-
|
|
278
|
-
```yaml
|
|
279
|
-
logger:
|
|
280
|
-
# app_name: "app"
|
|
281
|
-
level: "INFO"
|
|
282
|
-
use_diagnose: false
|
|
283
|
-
stream:
|
|
284
|
-
use_color: true
|
|
285
|
-
use_icon: false
|
|
286
|
-
format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
|
|
287
|
-
std_handler:
|
|
288
|
-
enabled: true
|
|
289
|
-
file:
|
|
290
|
-
logs_dir: "./logs"
|
|
291
|
-
rotate_size: 10000000 # 10MB
|
|
292
|
-
rotate_time: "00:00:00"
|
|
293
|
-
backup_count: 90
|
|
294
|
-
log_handlers:
|
|
295
|
-
enabled: false
|
|
296
|
-
format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
|
|
297
|
-
log_path: "{app_name}.std.all.log"
|
|
298
|
-
err_path: "{app_name}.std.err.log"
|
|
299
|
-
json_handlers:
|
|
300
|
-
enabled: false
|
|
301
|
-
use_custom: false
|
|
302
|
-
log_path: "{app_name}.json.all.log"
|
|
303
|
-
err_path: "{app_name}.json.err.log"
|
|
304
|
-
intercept:
|
|
305
|
-
auto_load:
|
|
306
|
-
enabled: true
|
|
307
|
-
only_base: false
|
|
308
|
-
ignore_modules: []
|
|
309
|
-
include_modules: []
|
|
310
|
-
mute_modules: []
|
|
311
|
-
extra:
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
## Documentation
|
|
315
|
-
|
|
316
|
-
- [docs](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/README.md)
|
|
317
|
-
- [scripts](https://github.com/bybatkhuu/module.python-logging/blob/main/docs/scripts/README.md)
|
|
318
|
-
|
|
319
|
-
---
|
|
320
|
-
|
|
321
|
-
## References
|
|
322
|
-
|
|
323
|
-
- <https://github.com/Delgan/loguru>
|
|
324
|
-
- <https://loguru.readthedocs.io/en/stable/api/logger.html>
|
|
325
|
-
- <https://loguru.readthedocs.io/en/stable/resources/recipes.html>
|
|
326
|
-
- <https://github.com/bybatkhuu/module.fastapi-logging>
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
beans_logging/__init__.py,sha256=TxuozMCLvGkG7sATP2uFCQws6TRqwp36yc08zUVGcb4,297
|
|
2
|
-
beans_logging/__version__.py,sha256=Skn1HZtKosF7bkVFNvpnIh3dEl9kV9eFp7ZGRzT6uQI,47
|
|
3
|
-
beans_logging/_base.py,sha256=HEf9Vyyj3oS0m5wuKd50LQDCVZNmsBEM1LntrYbnGjs,24571
|
|
4
|
-
beans_logging/_consts.py,sha256=b_-uytryZFCxlD2aKXzdS0oQpAsaK6JLi-w5bUeG1yE,345
|
|
5
|
-
beans_logging/_handlers.py,sha256=WQjr-dGAnjsegI2LHJl_Fubxb0reSCr8A6IvR7eN8dk,1133
|
|
6
|
-
beans_logging/_utils.py,sha256=Smb0cxkTdVxA-IE-oqi3sGhpsQdtNquQSw2bQxN0pDM,2742
|
|
7
|
-
beans_logging/auto.py,sha256=Wyi4w4qZ9PNW2dDF19E8qoKuZVfhyAEl_rVPzMscVCM,534
|
|
8
|
-
beans_logging/filters.py,sha256=YNUF5_0hE0XoPfLCi5wQFe1Qi7eLV0UaJUtCDwlG5ec,3369
|
|
9
|
-
beans_logging/formats.py,sha256=gxyJT9ZUWQvjrL4x6fU5bLKybGsFkCpFDG5OpndqDhc,1349
|
|
10
|
-
beans_logging/rotation.py,sha256=GspZX7wff_igZjbGSysCboz8fUgDHofGRL10OaNlZ3I,2137
|
|
11
|
-
beans_logging/schemas.py,sha256=syMsmwbDvDE1odnaIX18PEIEpWyItfDDnisZr1AsOPs,5641
|
|
12
|
-
beans_logging/sinks.py,sha256=C_y53i_QJuNZs_zBitb87d_tfsLhin2D9DtImPV5OHg,356
|
|
13
|
-
tests/__init__.py,sha256=iwhKnzeBJLKxpRVjvzwiRE63_zNpIBfaKLITauVph-0,24
|
|
14
|
-
tests/conftest.py,sha256=ycEL83-UMU-fcXQUZWTCNEPcBOZ38pzhoCPpXpCjIEM,319
|
|
15
|
-
tests/test_beans_logging.py,sha256=qyiM24QEAi7lVs-QBmjh_e48bCHk8nNYwMOlZIb6phw,1733
|
|
16
|
-
beans_logging-6.0.0.dist-info/LICENSE.txt,sha256=8jrXqC7FZbke39LPGo_mUFR81CkoUCP_vyefZjlQDOg,1074
|
|
17
|
-
beans_logging-6.0.0.dist-info/METADATA,sha256=jd6oaW9K3_v7vYmOt3RnGf3HODeTwkGrERzxwSCsMCM,9805
|
|
18
|
-
beans_logging-6.0.0.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
|
|
19
|
-
beans_logging-6.0.0.dist-info/top_level.txt,sha256=wSVo6vZwIqyOwwsbVBQceBQ_VJKuErw8OQvDiHcp8uU,20
|
|
20
|
-
beans_logging-6.0.0.dist-info/RECORD,,
|
tests/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
tests/conftest.py
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
|
|
5
|
-
from beans_logging import logger
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
@pytest.fixture(scope="session", autouse=True)
|
|
9
|
-
def setup_and_teardown():
|
|
10
|
-
# Equivalent of setUp
|
|
11
|
-
logger.info("Setting up...")
|
|
12
|
-
|
|
13
|
-
yield # This is where the testing happens!
|
|
14
|
-
|
|
15
|
-
# Equivalent of tearDown
|
|
16
|
-
logger.info("Tearing down!")
|
tests/test_beans_logging.py
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
|
|
5
|
-
from beans_logging import Logger, LoggerConfigPM, LoggerLoader
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
@pytest.fixture
|
|
9
|
-
def logger_loader():
|
|
10
|
-
_logger_loader = LoggerLoader()
|
|
11
|
-
|
|
12
|
-
yield _logger_loader
|
|
13
|
-
|
|
14
|
-
del _logger_loader
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@pytest.fixture
|
|
18
|
-
def logger():
|
|
19
|
-
from beans_logging import logger
|
|
20
|
-
|
|
21
|
-
yield logger
|
|
22
|
-
|
|
23
|
-
del logger
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def test_init(logger: Logger, logger_loader: LoggerLoader):
|
|
27
|
-
logger.info("Testing initialization of 'LoggerLoader'...")
|
|
28
|
-
|
|
29
|
-
assert isinstance(logger_loader, LoggerLoader)
|
|
30
|
-
assert logger_loader.handlers_map == {"default": 0}
|
|
31
|
-
assert logger_loader.config_file_path == LoggerLoader._CONFIG_FILE_PATH
|
|
32
|
-
assert isinstance(logger_loader.config, LoggerConfigPM)
|
|
33
|
-
|
|
34
|
-
logger.success("Done: Initialization of 'LoggerLoader'.\n")
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def test_load(logger: Logger, logger_loader: LoggerLoader):
|
|
38
|
-
logger.info("Testing 'load' method of 'LoggerLoader'...")
|
|
39
|
-
|
|
40
|
-
logger_loader.update_config(config={"level": "TRACE"})
|
|
41
|
-
_logger: Logger = logger_loader.load()
|
|
42
|
-
|
|
43
|
-
assert isinstance(_logger, Logger)
|
|
44
|
-
assert _logger == logger
|
|
45
|
-
_logger.trace("Tracing...")
|
|
46
|
-
_logger.debug("Debugging...")
|
|
47
|
-
_logger.info("Logging info.")
|
|
48
|
-
_logger.success("Success.")
|
|
49
|
-
_logger.warning("Warning something.")
|
|
50
|
-
_logger.error("Error occured.")
|
|
51
|
-
_logger.critical("CRITICAL ERROR.")
|
|
52
|
-
|
|
53
|
-
logger.success("Done: 'load' method.\n")
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def test_methods(logger: Logger):
|
|
57
|
-
logger.info("Testing 'logger' methods...")
|
|
58
|
-
|
|
59
|
-
logger.trace("Tracing...")
|
|
60
|
-
logger.debug("Debugging...")
|
|
61
|
-
logger.info("Logging info.")
|
|
62
|
-
logger.success("Success.")
|
|
63
|
-
logger.warning("Warning something.")
|
|
64
|
-
logger.error("Error occured.")
|
|
65
|
-
logger.critical("CRITICAL ERROR.")
|
|
66
|
-
|
|
67
|
-
logger.success("Done: 'logger' methods.\n")
|