structlog-config 0.4.2__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.
@@ -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
- level_map = logging.getLevelNamesMapping()
26
- left_level = level_map.get(left, left)
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
- # 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):
30
+ if left_level is None or right_level is None:
31
31
  raise ValueError(
32
- f"Invalid log level comparison: {left} ({type(left_level)}) vs {right} ({type(right_level)})"
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.4.2
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
@@ -4,11 +4,11 @@ structlog_config/env_config.py,sha256=_EJO0rgAKndRPSh4wuBaH3bui9F3nIpn8FaEkjAjZs
4
4
  structlog_config/environments.py,sha256=JpZYVVDGxEf1EaKdPdn6Jo-4wJK6SqF0ueFl7e2TBvI,612
5
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=z1fTpvCCbAwcFK2k7rHWh_p-FqfFh4yIWCTZ1MNf_4U,993
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.4.2.dist-info/WHEEL,sha256=4n27za1eEkOnA7dNjN6C5-O2rUiw6iapszm14Uj-Qmk,79
13
- structlog_config-0.4.2.dist-info/METADATA,sha256=wDb3zt_g4fwx5MtN2SvuXqi6O4FzDA-WH7TnYZNoUcg,6329
14
- structlog_config-0.4.2.dist-info/RECORD,,
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,,