mh_structlog 0.0.44__tar.gz → 0.0.45__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: mh_structlog
3
- Version: 0.0.44
3
+ Version: 0.0.45
4
4
  Summary: Some Structlog configuration and wrappers to easily use structlog.
5
5
  Author: Mathieu Hinderyckx
6
6
  Author-email: Mathieu Hinderyckx <mathieu.hinderyckx@gmail.com>
@@ -8,7 +8,7 @@ authors = [
8
8
  { name = "Mathieu Hinderyckx", email = "mathieu.hinderyckx@gmail.com" },
9
9
  ]
10
10
  description = "Some Structlog configuration and wrappers to easily use structlog."
11
- version = "0.0.44"
11
+ version = "0.0.45"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.10"
14
14
  classifiers = [
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import logging # noqa: I001
4
4
  import logging.config
5
+ import os
5
6
  import sys
6
7
  from pathlib import Path
7
8
  from typing import Literal
@@ -31,6 +32,7 @@ def setup( # noqa: PLR0912, PLR0915, C901
31
32
  max_frames: int = 100,
32
33
  sentry_config: dict | None = None,
33
34
  additional_processors: list | None = None, # noqa: FBT001, FBT002
35
+ timestamp_ms_precision: bool | None = True,
34
36
  ) -> None:
35
37
  """This method configures structlog and the standard library logging module."""
36
38
  global SELECTED_LOG_FORMAT # noqa: PLW0603
@@ -50,6 +52,9 @@ def setup( # noqa: PLR0912, PLR0915, C901
50
52
  structlog.contextvars.merge_contextvars, # add variables and bound data from global context
51
53
  ]
52
54
 
55
+ if timestamp_ms_precision:
56
+ shared_processors.append(processors.cap_timestamp_to_ms_precision)
57
+
53
58
  if additional_processors:
54
59
  shared_processors.extend(additional_processors)
55
60
 
@@ -99,8 +104,33 @@ def setup( # noqa: PLR0912, PLR0915, C901
99
104
  )
100
105
 
101
106
  wrapper_class = structlog.stdlib.BoundLogger
107
+
108
+ env_log_level_str = os.environ.get('LOG_LEVEL', '').upper()
109
+ env_log_level_constant = logging._nameToLevel.get(env_log_level_str) # noqa: SLF001
110
+
111
+ if env_log_level_str and not env_log_level_constant:
112
+ raise StructlogLoggingConfigExceptionError(
113
+ f"LOG_LEVEL environment variable has unrecognized value: {env_log_level_str}"
114
+ )
115
+
102
116
  if global_filter_level is not None:
117
+ if global_filter_level not in logging._nameToLevel.values(): # noqa: SLF001
118
+ raise StructlogLoggingConfigExceptionError(
119
+ f"global_filter_level has unrecognized value: {global_filter_level}"
120
+ )
121
+ if env_log_level_constant and env_log_level_constant != global_filter_level:
122
+ from logging import getLogger # noqa: PLC0415
123
+
124
+ getLogger('mh_structlog').warning(
125
+ 'Both global_filter_level (%s, %s) and LOG_LEVEL environment variable (%s, %s) are set, but they differ. global_filter_level takes precedence.',
126
+ global_filter_level,
127
+ logging.getLevelName(global_filter_level),
128
+ env_log_level_constant,
129
+ logging.getLevelName(env_log_level_constant),
130
+ )
103
131
  wrapper_class = structlog.make_filtering_bound_logger(global_filter_level)
132
+ elif env_log_level_constant:
133
+ wrapper_class = structlog.make_filtering_bound_logger(env_log_level_constant) # noqa: SLF001
104
134
 
105
135
  # Structlog configuration
106
136
  structlog.configure(
@@ -95,3 +95,10 @@ class CapExceptionFrames:
95
95
  if self.max_frames is not None and 'exception' in event_dict and 'frames' in event_dict["exception"]:
96
96
  event_dict['exception']['frames'] = event_dict['exception']['frames'][-self.max_frames :]
97
97
  return event_dict
98
+
99
+
100
+ def cap_timestamp_to_ms_precision(_, __, event_dict: dict) -> dict: # noqa: ANN001
101
+ """Cap the timestamp to millisecond precision, dropping the microseconds part."""
102
+ if ts := event_dict.get("timestamp"):
103
+ event_dict['timestamp'] = ts[:-4] + 'Z'
104
+ return event_dict
File without changes