izihawa-loglib 1.1.17__tar.gz → 1.1.19__tar.gz
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-1.1.17 → izihawa_loglib-1.1.19}/PKG-INFO +1 -1
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/izihawa_loglib/formatters.py +7 -11
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/izihawa_loglib/request_context.py +3 -10
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/pyproject.toml +1 -1
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/README.md +0 -0
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/izihawa_loglib/__init__.py +0 -0
- {izihawa_loglib-1.1.17 → izihawa_loglib-1.1.19}/izihawa_loglib/handlers.py +0 -0
@@ -16,19 +16,13 @@ DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"
|
|
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
20
|
elif isinstance(record.msg, typing.Dict) or dataclasses.is_dataclass(
|
21
21
|
record.msg
|
22
22
|
):
|
23
|
-
|
23
|
+
return record.msg
|
24
24
|
else:
|
25
|
-
|
26
|
-
|
27
|
-
client_ip = getattr(record, "client_ip", None)
|
28
|
-
if client_ip:
|
29
|
-
d["client_ip"] = str(client_ip)
|
30
|
-
|
31
|
-
return d
|
25
|
+
return dict(message=super().format(record))
|
32
26
|
|
33
27
|
def format(self, record):
|
34
28
|
log_record = self._prepare(record)
|
@@ -43,6 +37,7 @@ class DefaultHttpFormatter(BaseFormatter):
|
|
43
37
|
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
44
38
|
DATETIME_FORMAT
|
45
39
|
)
|
40
|
+
client_ip = getattr(record, "client_ip", None)
|
46
41
|
request_id = getattr(record, "request_id", None)
|
47
42
|
method = getattr(record, "method", None)
|
48
43
|
path = getattr(record, "path", None)
|
@@ -54,6 +49,8 @@ class DefaultHttpFormatter(BaseFormatter):
|
|
54
49
|
process=os.getpid(),
|
55
50
|
)
|
56
51
|
|
52
|
+
if client_ip:
|
53
|
+
log_record["client_ip"] = client_ip
|
57
54
|
if request_id:
|
58
55
|
log_record["request_id"] = request_id
|
59
56
|
if method:
|
@@ -76,7 +73,6 @@ class DefaultFormatter(BaseFormatter):
|
|
76
73
|
formatted_datetime = datetime.datetime.fromtimestamp(timestamp).strftime(
|
77
74
|
DATETIME_FORMAT
|
78
75
|
)
|
79
|
-
|
80
76
|
log_record.update(
|
81
77
|
unixtime=int(timestamp),
|
82
78
|
timestamp=int(timestamp * 1_000_000),
|
@@ -87,7 +83,7 @@ class DefaultFormatter(BaseFormatter):
|
|
87
83
|
|
88
84
|
def format(self, record):
|
89
85
|
log_record = self._prepare(record)
|
90
|
-
return json.dumps(log_record).decode()
|
86
|
+
return json.dumps(log_record, default=str).decode()
|
91
87
|
|
92
88
|
|
93
89
|
class TracebackFormatter(DefaultFormatter):
|
@@ -9,7 +9,8 @@ class RequestContext:
|
|
9
9
|
request_id_length: int = 12
|
10
10
|
|
11
11
|
def __init__(
|
12
|
-
self,
|
12
|
+
self,
|
13
|
+
**kwargs: Any,
|
13
14
|
):
|
14
15
|
self.default_fields = kwargs
|
15
16
|
if not self.default_fields.get("request_id"):
|
@@ -22,7 +23,7 @@ class RequestContext:
|
|
22
23
|
return self.default_fields[name]
|
23
24
|
except KeyError:
|
24
25
|
classname = type(self).__name__
|
25
|
-
msg = f
|
26
|
+
msg = f"{classname!r} object has no attribute {name!r}"
|
26
27
|
raise AttributeError(msg)
|
27
28
|
|
28
29
|
@staticmethod
|
@@ -44,11 +45,3 @@ class RequestContext:
|
|
44
45
|
def error_log(self, e, level=logging.ERROR, **fields) -> None:
|
45
46
|
all_fields = {**self.default_fields, **fields}
|
46
47
|
error_log(e, level=level, **all_fields)
|
47
|
-
|
48
|
-
def to_default_headers(self) -> dict[str, Any]:
|
49
|
-
headers = {}
|
50
|
-
if request_id := self.default_fields.get("request_id"):
|
51
|
-
headers["X-Request-Id"] = request_id
|
52
|
-
if client_id := self.default_fields.get("client_id"):
|
53
|
-
headers["X-Client-Id"] = client_id
|
54
|
-
return headers
|
File without changes
|
File without changes
|
File without changes
|