structlog-config 0.4.0__py3-none-any.whl → 0.4.2__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.
@@ -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
- log = structlog.get_logger("access_log")
18
+ # should name this access "access_log" or something
19
+ log = structlog.get_logger()
15
20
  ipw = IpWare()
16
21
 
17
22
 
@@ -50,22 +55,6 @@ def get_path_with_query_string(scope: Scope) -> str:
50
55
  return path_with_query_string
51
56
 
52
57
 
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
58
  def client_ip_from_request(request: Request | WebSocket) -> str | None:
70
59
  """
71
60
  Get the client IP address from the request.
@@ -125,6 +114,8 @@ def is_static_assets_request(scope: Scope) -> bool:
125
114
  )
126
115
 
127
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?
128
119
  def add_middleware(
129
120
  app: FastAPI,
130
121
  ) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: structlog-config
3
- Version: 0.4.0
3
+ Version: 0.4.2
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.
@@ -2,13 +2,13 @@ structlog_config/__init__.py,sha256=DyY4x3_dY_hPNbS1aM7JRCGadTa1dYDIPzgrHu3AP68,
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=REwchxGb5WHbiQi2zYtBPfkGHk-NvCKM2pVwJlwG5H8,5464
5
+ structlog_config/fastapi_access_logger.py,sha256=QuvteKBZGsEiVaPEpdpsK1jjCYEPKnhmsQDC89M2JKc,5452
6
6
  structlog_config/formatters.py,sha256=ll0Y0QeRs1DMmD-ft1n1zA4Vn2STRSK-mOrczYB2OjE,5898
7
7
  structlog_config/levels.py,sha256=z1fTpvCCbAwcFK2k7rHWh_p-FqfFh4yIWCTZ1MNf_4U,993
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.0.dist-info/WHEEL,sha256=4n27za1eEkOnA7dNjN6C5-O2rUiw6iapszm14Uj-Qmk,79
13
- structlog_config-0.4.0.dist-info/METADATA,sha256=R2XtpZuBtPdum5qHcmmCoXbSFoQPm0iv4VQcAX0I7zI,5472
14
- structlog_config-0.4.0.dist-info/RECORD,,
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,,