structlog-config 0.4.1__py3-none-any.whl → 0.5.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.
- structlog_config/__init__.py +0 -3
- structlog_config/fastapi_access_logger.py +8 -1
- structlog_config/levels.py +49 -6
- {structlog_config-0.4.1.dist-info → structlog_config-0.5.0.dist-info}/METADATA +1 -1
- {structlog_config-0.4.1.dist-info → structlog_config-0.5.0.dist-info}/RECORD +6 -6
- {structlog_config-0.4.1.dist-info → structlog_config-0.5.0.dist-info}/WHEEL +0 -0
structlog_config/__init__.py
CHANGED
|
@@ -9,9 +9,6 @@ from structlog.processors import ExceptionRenderer
|
|
|
9
9
|
from structlog.tracebacks import ExceptionDictTransformer
|
|
10
10
|
from structlog.typing import FilteringBoundLogger
|
|
11
11
|
|
|
12
|
-
from structlog_config.fastapi_access_logger import (
|
|
13
|
-
client_ip_from_request,
|
|
14
|
-
)
|
|
15
12
|
from structlog_config.formatters import (
|
|
16
13
|
PathPrettifier,
|
|
17
14
|
add_fastapi_context,
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Requires fastapi and is not loaded by default since fastapi is not a default dependency.
|
|
3
|
+
"""
|
|
4
|
+
|
|
1
5
|
from time import perf_counter
|
|
2
6
|
from urllib.parse import quote
|
|
3
7
|
|
|
@@ -11,7 +15,8 @@ from starlette.routing import Match, Mount
|
|
|
11
15
|
from starlette.types import Scope
|
|
12
16
|
from starlette.websockets import WebSocket
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
# should name this access "access_log" or something
|
|
19
|
+
log = structlog.get_logger()
|
|
15
20
|
ipw = IpWare()
|
|
16
21
|
|
|
17
22
|
|
|
@@ -109,6 +114,8 @@ def is_static_assets_request(scope: Scope) -> bool:
|
|
|
109
114
|
)
|
|
110
115
|
|
|
111
116
|
|
|
117
|
+
# TODO issue with this approach is if there is an error we don't get a path logged :/
|
|
118
|
+
# maybe we should log a ERROR with the path information and wrap it with a try?
|
|
112
119
|
def add_middleware(
|
|
113
120
|
app: FastAPI,
|
|
114
121
|
) -> None:
|
structlog_config/levels.py
CHANGED
|
@@ -2,6 +2,8 @@ import logging
|
|
|
2
2
|
|
|
3
3
|
from decouple import config
|
|
4
4
|
|
|
5
|
+
from .constants import TRACE_LOG_LEVEL, package_logger
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
def get_environment_log_level_as_string() -> str:
|
|
7
9
|
level = config("LOG_LEVEL", default="INFO", cast=str).upper()
|
|
@@ -22,14 +24,55 @@ def compare_log_levels(left: str, right: str) -> int:
|
|
|
22
24
|
|
|
23
25
|
Asks the question "Is INFO higher than DEBUG?"
|
|
24
26
|
"""
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
right_level = level_map.get(right, right)
|
|
27
|
+
left_level = _resolve_level_name(left)
|
|
28
|
+
right_level = _resolve_level_name(right)
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
if not isinstance(left_level, int) or not isinstance(right_level, int):
|
|
30
|
+
if left_level is None or right_level is None:
|
|
31
31
|
raise ValueError(
|
|
32
|
-
f"Invalid log level comparison: {left} ({
|
|
32
|
+
f"Invalid log level comparison: {left} ({left_level}) vs {right} ({right_level})"
|
|
33
33
|
)
|
|
34
34
|
|
|
35
35
|
return left_level - right_level
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _resolve_level_name(level_name: str) -> int | None:
|
|
39
|
+
"""Translate a log level name to its numeric value."""
|
|
40
|
+
level_map = logging.getLevelNamesMapping()
|
|
41
|
+
resolved = level_map.get(level_name)
|
|
42
|
+
|
|
43
|
+
if isinstance(resolved, int):
|
|
44
|
+
return resolved
|
|
45
|
+
|
|
46
|
+
if level_name == "TRACE":
|
|
47
|
+
return getattr(logging, "TRACE", TRACE_LOG_LEVEL)
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
return int(level_name)
|
|
51
|
+
except (TypeError, ValueError):
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def is_debug_level() -> bool:
|
|
56
|
+
"""
|
|
57
|
+
Return True when the global logger is configured for DEBUG or TRACE verbosity.
|
|
58
|
+
|
|
59
|
+
Helpful for enabling `debug` flags on various 3rd party libraries. This makes it easy to turn
|
|
60
|
+
on debug modes globally via LOG_LEVEL environment variable.
|
|
61
|
+
"""
|
|
62
|
+
root_logger = logging.getLogger()
|
|
63
|
+
current_level = root_logger.getEffectiveLevel()
|
|
64
|
+
|
|
65
|
+
if current_level == logging.NOTSET:
|
|
66
|
+
# Root loggers default to NOTSET until configured. In that state logging falls back to
|
|
67
|
+
# environment configuration, so resolve the env value manually to avoid treating it as INFO.
|
|
68
|
+
package_logger.warning(
|
|
69
|
+
"Detected root logger level logging.NOTSET; falling back to LOG_LEVEL env value."
|
|
70
|
+
)
|
|
71
|
+
env_level = _resolve_level_name(get_environment_log_level_as_string())
|
|
72
|
+
if env_level is None:
|
|
73
|
+
return False
|
|
74
|
+
current_level = env_level
|
|
75
|
+
|
|
76
|
+
debug_level = _resolve_level_name("DEBUG") or logging.DEBUG
|
|
77
|
+
|
|
78
|
+
return current_level <= debug_level
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: structlog-config
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: A comprehensive structlog configuration with sensible defaults for development and production environments, featuring context management, exception formatting, and path prettification.
|
|
5
5
|
Keywords: logging,structlog,json-logging,structured-logging
|
|
6
6
|
Author: Michael Bianco
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
structlog_config/__init__.py,sha256=
|
|
1
|
+
structlog_config/__init__.py,sha256=DyY4x3_dY_hPNbS1aM7JRCGadTa1dYDIPzgrHu3AP68,6733
|
|
2
2
|
structlog_config/constants.py,sha256=O1nPnB29yZdqqaI7aeTUrimA3LOtA5WpP6BGPLWJvj8,510
|
|
3
3
|
structlog_config/env_config.py,sha256=_EJO0rgAKndRPSh4wuBaH3bui9F3nIpn8FaEkjAjZso,1737
|
|
4
4
|
structlog_config/environments.py,sha256=JpZYVVDGxEf1EaKdPdn6Jo-4wJK6SqF0ueFl7e2TBvI,612
|
|
5
|
-
structlog_config/fastapi_access_logger.py,sha256=
|
|
5
|
+
structlog_config/fastapi_access_logger.py,sha256=QuvteKBZGsEiVaPEpdpsK1jjCYEPKnhmsQDC89M2JKc,5452
|
|
6
6
|
structlog_config/formatters.py,sha256=ll0Y0QeRs1DMmD-ft1n1zA4Vn2STRSK-mOrczYB2OjE,5898
|
|
7
|
-
structlog_config/levels.py,sha256=
|
|
7
|
+
structlog_config/levels.py,sha256=LqXG4l01mIpxS2qI7PF_Vp9K7IrO0D5qU_x-Uo3LNuM,2372
|
|
8
8
|
structlog_config/packages.py,sha256=asxrzLR-iRYAbkoSYutyTdIRcruTjHgkzfe2pjm2VFM,519
|
|
9
9
|
structlog_config/stdlib_logging.py,sha256=Wnn59oRBIqn708CpR-akqVcG9ccSfCMLh56_7wxZRH0,7350
|
|
10
10
|
structlog_config/trace.py,sha256=dBaSynxmw4Wg79wSHqYEMoByvv--v_oQw61dRdg4xUI,2016
|
|
11
11
|
structlog_config/warnings.py,sha256=gKEcuHWqH0BaKitJtQkv-uJ0Z3uCH5nn6k8qpqjR-RM,998
|
|
12
|
-
structlog_config-0.
|
|
13
|
-
structlog_config-0.
|
|
14
|
-
structlog_config-0.
|
|
12
|
+
structlog_config-0.5.0.dist-info/WHEEL,sha256=4n27za1eEkOnA7dNjN6C5-O2rUiw6iapszm14Uj-Qmk,79
|
|
13
|
+
structlog_config-0.5.0.dist-info/METADATA,sha256=d1CeOq9mFTIcBP80JKqnGZ_mEHud_5ooaI-6KDn51mM,6329
|
|
14
|
+
structlog_config-0.5.0.dist-info/RECORD,,
|
|
File without changes
|