structlog-config 0.4.0__tar.gz → 0.4.1__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: structlog-config
3
- Version: 0.4.0
3
+ Version: 0.4.1
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
@@ -39,6 +39,27 @@ log = configure_logger()
39
39
  log.info("the log", key="value")
40
40
  ```
41
41
 
42
+ ## JSON Logging for Production
43
+
44
+ JSON logging is automatically enabled in production and staging environments (`PYTHON_ENV=production` or `PYTHON_ENV=staging`):
45
+
46
+ ```python
47
+ from structlog_config import configure_logger
48
+
49
+ # Automatic JSON logging in production
50
+ log = configure_logger()
51
+ log.info("User login", user_id="123", action="login")
52
+ # Output: {"action":"login","event":"User login","level":"info","timestamp":"2025-09-24T18:03:00Z","user_id":"123"}
53
+
54
+ # Force JSON logging regardless of environment
55
+ log = configure_logger(json_logger=True)
56
+
57
+ # Force console logging regardless of environment
58
+ log = configure_logger(json_logger=False)
59
+ ```
60
+
61
+ JSON logs use [orjson](https://github.com/ijl/orjson) for performance, include sorted keys and ISO timestamps, and serialize exceptions cleanly. Note that `PYTHON_LOG_PATH` is ignored with JSON logging (stdout only).
62
+
42
63
  ## TRACE Logging Level
43
64
 
44
65
  This package adds support for a custom `TRACE` logging level (level 5) that's even more verbose than `DEBUG`. This is useful for extremely detailed debugging scenarios.
@@ -24,6 +24,27 @@ log = configure_logger()
24
24
  log.info("the log", key="value")
25
25
  ```
26
26
 
27
+ ## JSON Logging for Production
28
+
29
+ JSON logging is automatically enabled in production and staging environments (`PYTHON_ENV=production` or `PYTHON_ENV=staging`):
30
+
31
+ ```python
32
+ from structlog_config import configure_logger
33
+
34
+ # Automatic JSON logging in production
35
+ log = configure_logger()
36
+ log.info("User login", user_id="123", action="login")
37
+ # Output: {"action":"login","event":"User login","level":"info","timestamp":"2025-09-24T18:03:00Z","user_id":"123"}
38
+
39
+ # Force JSON logging regardless of environment
40
+ log = configure_logger(json_logger=True)
41
+
42
+ # Force console logging regardless of environment
43
+ log = configure_logger(json_logger=False)
44
+ ```
45
+
46
+ JSON logs use [orjson](https://github.com/ijl/orjson) for performance, include sorted keys and ISO timestamps, and serialize exceptions cleanly. Note that `PYTHON_LOG_PATH` is ignored with JSON logging (stdout only).
47
+
27
48
  ## TRACE Logging Level
28
49
 
29
50
  This package adds support for a custom `TRACE` logging level (level 5) that's even more verbose than `DEBUG`. This is useful for extremely detailed debugging scenarios.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "structlog-config"
3
- version = "0.4.0"
3
+ version = "0.4.1"
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"
@@ -9,6 +9,9 @@ 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
+ )
12
15
  from structlog_config.formatters import (
13
16
  PathPrettifier,
14
17
  add_fastapi_context,
@@ -50,22 +50,6 @@ def get_path_with_query_string(scope: Scope) -> str:
50
50
  return path_with_query_string
51
51
 
52
52
 
53
- def get_client_addr(scope: Scope) -> str:
54
- """Get the client's address.
55
-
56
- Args:
57
- scope (Scope): Current context.
58
-
59
- Returns:
60
- str: Client's address in the IP:PORT format.
61
- """
62
- client = scope.get("client")
63
- if not client:
64
- return ""
65
- ip, port = client
66
- return f"{ip}:{port}"
67
-
68
-
69
53
  def client_ip_from_request(request: Request | WebSocket) -> str | None:
70
54
  """
71
55
  Get the client IP address from the request.