structlog-config 0.4.2__tar.gz → 0.5.0__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.
- {structlog_config-0.4.2 → structlog_config-0.5.0}/PKG-INFO +1 -1
- {structlog_config-0.4.2 → structlog_config-0.5.0}/pyproject.toml +1 -1
- structlog_config-0.5.0/structlog_config/levels.py +78 -0
- structlog_config-0.4.2/structlog_config/levels.py +0 -35
- {structlog_config-0.4.2 → structlog_config-0.5.0}/README.md +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/__init__.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/constants.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/env_config.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/environments.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/fastapi_access_logger.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/formatters.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/packages.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/stdlib_logging.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/trace.py +0 -0
- {structlog_config-0.4.2 → structlog_config-0.5.0}/structlog_config/warnings.py +0 -0
|
@@ -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,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "structlog-config"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0"
|
|
4
4
|
description = "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
|
readme = "README.md"
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from decouple import config
|
|
4
|
+
|
|
5
|
+
from .constants import TRACE_LOG_LEVEL, package_logger
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_environment_log_level_as_string() -> str:
|
|
9
|
+
level = config("LOG_LEVEL", default="INFO", cast=str).upper()
|
|
10
|
+
|
|
11
|
+
if not level.strip():
|
|
12
|
+
level = "INFO"
|
|
13
|
+
|
|
14
|
+
return level
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def compare_log_levels(left: str, right: str) -> int:
|
|
18
|
+
"""
|
|
19
|
+
Compare log levels using logging.getLevelNamesMapping for accurate int values.
|
|
20
|
+
|
|
21
|
+
Example:
|
|
22
|
+
>>> compare_log_levels("DEBUG", "INFO")
|
|
23
|
+
-1 # DEBUG is less than INFO
|
|
24
|
+
|
|
25
|
+
Asks the question "Is INFO higher than DEBUG?"
|
|
26
|
+
"""
|
|
27
|
+
left_level = _resolve_level_name(left)
|
|
28
|
+
right_level = _resolve_level_name(right)
|
|
29
|
+
|
|
30
|
+
if left_level is None or right_level is None:
|
|
31
|
+
raise ValueError(
|
|
32
|
+
f"Invalid log level comparison: {left} ({left_level}) vs {right} ({right_level})"
|
|
33
|
+
)
|
|
34
|
+
|
|
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,35 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
|
-
from decouple import config
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def get_environment_log_level_as_string() -> str:
|
|
7
|
-
level = config("LOG_LEVEL", default="INFO", cast=str).upper()
|
|
8
|
-
|
|
9
|
-
if not level.strip():
|
|
10
|
-
level = "INFO"
|
|
11
|
-
|
|
12
|
-
return level
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def compare_log_levels(left: str, right: str) -> int:
|
|
16
|
-
"""
|
|
17
|
-
Compare log levels using logging.getLevelNamesMapping for accurate int values.
|
|
18
|
-
|
|
19
|
-
Example:
|
|
20
|
-
>>> compare_log_levels("DEBUG", "INFO")
|
|
21
|
-
-1 # DEBUG is less than INFO
|
|
22
|
-
|
|
23
|
-
Asks the question "Is INFO higher than DEBUG?"
|
|
24
|
-
"""
|
|
25
|
-
level_map = logging.getLevelNamesMapping()
|
|
26
|
-
left_level = level_map.get(left, left)
|
|
27
|
-
right_level = level_map.get(right, right)
|
|
28
|
-
|
|
29
|
-
# TODO should more gracefully fail here, but let's see what happens
|
|
30
|
-
if not isinstance(left_level, int) or not isinstance(right_level, int):
|
|
31
|
-
raise ValueError(
|
|
32
|
-
f"Invalid log level comparison: {left} ({type(left_level)}) vs {right} ({type(right_level)})"
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
return left_level - right_level
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|