izihawa-loglib 1.0.1__py3-none-any.whl → 1.1.1__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.
- izihawa_loglib/__init__.py +11 -14
- izihawa_loglib/formatters.py +19 -13
- izihawa_loglib/handlers.py +5 -3
- izihawa_loglib-1.1.1.dist-info/METADATA +20 -0
- izihawa_loglib-1.1.1.dist-info/RECORD +6 -0
- {izihawa_loglib-1.0.1.dist-info → izihawa_loglib-1.1.1.dist-info}/WHEEL +1 -1
- izihawa_loglib-1.0.1.dist-info/METADATA +0 -14
- izihawa_loglib-1.0.1.dist-info/RECORD +0 -6
izihawa_loglib/__init__.py
CHANGED
@@ -5,33 +5,30 @@ import sys
|
|
5
5
|
from izihawa_utils.exceptions import BaseError
|
6
6
|
from izihawa_utils.file import mkdir_p
|
7
7
|
|
8
|
-
from .formatters import DefaultFormatter, DefaultHttpFormatter
|
9
8
|
from .handlers import QueueHandler
|
10
9
|
|
11
10
|
|
12
|
-
def configure_logging(config, make_path=
|
13
|
-
if config.get(
|
14
|
-
logging.basicConfig(stream=sys.stdout, level=
|
11
|
+
def configure_logging(config: dict, make_path: bool = False, default_level=logging.INFO):
|
12
|
+
if config.get("application", {}).get("debug", False) or "logging" not in config:
|
13
|
+
logging.basicConfig(stream=sys.stdout, level=default_level)
|
15
14
|
else:
|
16
15
|
if make_path:
|
17
|
-
mkdir_p(config[
|
18
|
-
logging.config.dictConfig(config[
|
16
|
+
mkdir_p(config["log_path"])
|
17
|
+
logging.config.dictConfig(config["logging"])
|
19
18
|
|
20
19
|
|
21
20
|
def error_log(e, level=logging.ERROR, **fields):
|
22
|
-
level = getattr(e,
|
21
|
+
level = getattr(e, "level", level)
|
23
22
|
if isinstance(e, BaseError):
|
24
23
|
e = e.as_internal_dict()
|
25
24
|
e.update(fields)
|
26
25
|
elif fields:
|
27
|
-
e = {
|
28
|
-
logging.getLogger(
|
29
|
-
msg=str(e),
|
30
|
-
level=level
|
31
|
-
)
|
26
|
+
e = {"error": repr(e), **fields}
|
27
|
+
logging.getLogger("error").log(msg=str(e), level=level)
|
32
28
|
|
33
29
|
|
34
30
|
__all__ = [
|
35
|
-
|
36
|
-
|
31
|
+
"QueueHandler",
|
32
|
+
"configure_logging",
|
33
|
+
"error_log",
|
37
34
|
]
|
izihawa_loglib/formatters.py
CHANGED
@@ -10,14 +10,16 @@ import typing
|
|
10
10
|
import orjson as json
|
11
11
|
from izihawa_utils.exceptions import BaseError
|
12
12
|
|
13
|
-
DATETIME_FORMAT =
|
13
|
+
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"
|
14
14
|
|
15
15
|
|
16
16
|
class BaseFormatter(logging.Formatter):
|
17
17
|
def _prepare(self, record):
|
18
18
|
if isinstance(record.msg, BaseError):
|
19
19
|
return record.msg.as_internal_dict()
|
20
|
-
elif isinstance(record.msg, typing.Dict) or dataclasses.is_dataclass(
|
20
|
+
elif isinstance(record.msg, typing.Dict) or dataclasses.is_dataclass(
|
21
|
+
record.msg
|
22
|
+
):
|
21
23
|
return record.msg
|
22
24
|
else:
|
23
25
|
return dict(message=super().format(record))
|
@@ -32,11 +34,13 @@ class DefaultHttpFormatter(BaseFormatter):
|
|
32
34
|
log_record = super()._prepare(record)
|
33
35
|
|
34
36
|
timestamp = time.time()
|
35
|
-
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
38
|
+
DATETIME_FORMAT
|
39
|
+
)
|
40
|
+
user_ip = getattr(record, "user_ip", None)
|
41
|
+
request_id = getattr(record, "request_id", None)
|
42
|
+
method = getattr(record, "method", None)
|
43
|
+
path = getattr(record, "path", None)
|
40
44
|
|
41
45
|
log_record.update(
|
42
46
|
unixtime=int(timestamp),
|
@@ -46,13 +50,13 @@ class DefaultHttpFormatter(BaseFormatter):
|
|
46
50
|
)
|
47
51
|
|
48
52
|
if user_ip:
|
49
|
-
log_record[
|
53
|
+
log_record["user_ip"] = user_ip
|
50
54
|
if request_id:
|
51
|
-
log_record[
|
55
|
+
log_record["request_id"] = request_id
|
52
56
|
if method:
|
53
|
-
log_record[
|
57
|
+
log_record["method"] = method
|
54
58
|
if path:
|
55
|
-
log_record[
|
59
|
+
log_record["path"] = path
|
56
60
|
|
57
61
|
return log_record
|
58
62
|
|
@@ -66,7 +70,9 @@ class DefaultFormatter(BaseFormatter):
|
|
66
70
|
log_record = super()._prepare(record)
|
67
71
|
|
68
72
|
timestamp = time.time()
|
69
|
-
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
73
|
+
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
74
|
+
DATETIME_FORMAT
|
75
|
+
)
|
70
76
|
|
71
77
|
log_record.update(
|
72
78
|
unixtime=int(timestamp),
|
@@ -86,7 +92,7 @@ class TracebackFormatter(DefaultFormatter):
|
|
86
92
|
log_record = self._prepare(record)
|
87
93
|
value = pprint.pformat(log_record, indent=2)
|
88
94
|
if traceback.sys.exc_info()[0] is not None:
|
89
|
-
value +=
|
95
|
+
value += "\n" + traceback.format_exc()
|
90
96
|
return value
|
91
97
|
|
92
98
|
|
izihawa_loglib/handlers.py
CHANGED
@@ -8,8 +8,10 @@ from izihawa_types.var import varstr
|
|
8
8
|
class QueueHandler(logging.handlers.QueueHandler):
|
9
9
|
def __init__(self, *handlers):
|
10
10
|
self._queue = queue.Queue(-1)
|
11
|
-
self._listener = logging.handlers.QueueListener(
|
12
|
-
|
11
|
+
self._listener = logging.handlers.QueueListener(
|
12
|
+
self._queue, *handlers, respect_handler_level=True
|
13
|
+
)
|
14
|
+
self.setLevel("INFO")
|
13
15
|
|
14
16
|
super().__init__(self._queue)
|
15
17
|
self._listener.start()
|
@@ -30,7 +32,7 @@ class BaseFileHandler(logging.handlers.WatchedFileHandler):
|
|
30
32
|
|
31
33
|
class BaseBinaryFileHandler(BaseFileHandler):
|
32
34
|
def __init__(self, *args, **kwargs):
|
33
|
-
super().__init__(*args, **kwargs, mode=
|
35
|
+
super().__init__(*args, **kwargs, mode="ab+")
|
34
36
|
|
35
37
|
def emit(self, record):
|
36
38
|
try:
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: izihawa_loglib
|
3
|
+
Version: 1.1.1
|
4
|
+
Summary: Izihawa log utilities
|
5
|
+
Author: Pasha Podolsky
|
6
|
+
Author-email: ppodolsky@me.com
|
7
|
+
Requires-Python: >=3.8.1,<3.13
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
13
|
+
Requires-Dist: confuse (>=2.0.1,<3.0.0)
|
14
|
+
Requires-Dist: izihawa-types (>=0.1.4,<0.2.0)
|
15
|
+
Requires-Dist: izihawa-utils (>=1.2.1,<2.0.0)
|
16
|
+
Requires-Dist: orjson (>=3.9.10,<4.0.0)
|
17
|
+
Description-Content-Type: text/markdown
|
18
|
+
|
19
|
+
# izihawa_netlib
|
20
|
+
|
@@ -0,0 +1,6 @@
|
|
1
|
+
izihawa_loglib/__init__.py,sha256=qrxMsrTCPI8_CmvEGdhNliUKSQwkHD3RpZsp5smynQk,918
|
2
|
+
izihawa_loglib/formatters.py,sha256=ZQj0FMJs4qJcaFKz__VSsrUGXgMcXhQKSTAqemWYZG8,2816
|
3
|
+
izihawa_loglib/handlers.py,sha256=UMBb7VSFvo4QUD0aueyzmh2ryq5tl8U4iKB9IIbax-0,1095
|
4
|
+
izihawa_loglib-1.1.1.dist-info/METADATA,sha256=SH9-fOUV96pbnP5DvkFprf-s6EcxhmWuUTimT2ErfxY,656
|
5
|
+
izihawa_loglib-1.1.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
6
|
+
izihawa_loglib-1.1.1.dist-info/RECORD,,
|
@@ -1,14 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.1
|
2
|
-
Name: izihawa-loglib
|
3
|
-
Version: 1.0.1
|
4
|
-
Author: Pasha Podolsky
|
5
|
-
Author-email: ppodolsky@me.com
|
6
|
-
Home-page: https://github.com/izihawa/loglib
|
7
|
-
License: MIT License
|
8
|
-
Classifier: Programming Language :: Python :: 3.8
|
9
|
-
Requires-Python: >=3.8
|
10
|
-
Requires-Dist: izihawa-types
|
11
|
-
Requires-Dist: izihawa-utils >= 1.0.2
|
12
|
-
Requires-Dist: orjson >= 3.6.8
|
13
|
-
|
14
|
-
UNKNOWN
|
@@ -1,6 +0,0 @@
|
|
1
|
-
izihawa_loglib-1.0.1.dist-info/METADATA,sha256=dnPc51xBz3833r-pCsdh13sm-1QME52dDxN5u7xmxmE,358
|
2
|
-
izihawa_loglib-1.0.1.dist-info/RECORD,,
|
3
|
-
izihawa_loglib-1.0.1.dist-info/WHEEL,sha256=sobxWSyDDkdg_rinUth-jxhXHqoNqlmNMJY3aTZn2Us,91
|
4
|
-
izihawa_loglib/__init__.py,sha256=Ato6-AjQ7I0QmVJNB2Qbic66WGeNtexar0lxl05i3gI,999
|
5
|
-
izihawa_loglib/formatters.py,sha256=fFpNdF9PjsW9sQgVDVwVfmFPnKkrbo1RTMvYxFRH114,2750
|
6
|
-
izihawa_loglib/handlers.py,sha256=PLqgMnP6Mvb9btW7iRtI5PGCRocTLeiYW5QaiNOEuzk,1073
|