litewave-logger 0.4.0__tar.gz → 0.6.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: litewave_logger
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: A lightweight logging library with context carry forward
5
5
  Author: Litewave
6
6
  Author-email: Sonu Sudhakaran <sonu@litewave.ai>
@@ -21,6 +21,7 @@ This module provides a centralized and consistent logging solution for Litewave
21
21
  - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
22
22
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
23
23
  - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
+ - **Flask Integration**: Middleware for Flask to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
25
  - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
25
26
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
26
27
  - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
@@ -53,6 +54,16 @@ To use the `litewave_logger` in your service, follow these steps:
53
54
  app.add_middleware(RequestIdMiddleware)
54
55
  ```
55
56
 
57
+ **Or for Flask applications**: If your service is a Flask application, use the `FlaskRequestIdMiddleware`.
58
+
59
+ ```python
60
+ from flask import Flask
61
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
62
+
63
+ app = Flask(__name__)
64
+ FlaskRequestIdMiddleware(app)
65
+ ```
66
+
56
67
  3. **Patch the `requests` library**: To ensure the `request-id` is propagated to other services, patch the `requests` library.
57
68
 
58
69
  ```python
@@ -68,7 +79,7 @@ To use the `litewave_logger` in your service, follow these steps:
68
79
  import litewave_logger.celery
69
80
  ```
70
81
 
71
- ### Example
82
+ ### FastAPI Example
72
83
 
73
84
  Here's a complete example of how to integrate the `litewave_logger` into a FastAPI application:
74
85
 
@@ -95,6 +106,53 @@ app.add_middleware(RequestIdMiddleware)
95
106
  # Your application code here...
96
107
  ```
97
108
 
109
+ ### Flask Example
110
+
111
+ Here's a complete example of how to integrate the `litewave_logger` into a Flask application:
112
+
113
+ ```python
114
+ from flask import Flask
115
+ from litewave_logger import setup_logging
116
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
117
+ from litewave_logger.requests import patch_requests
118
+
119
+ # Import Celery module to register signal handlers (if using Celery)
120
+ import litewave_logger.celery
121
+
122
+ # 1. Initialize logging (optionally exclude endpoints)
123
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
124
+
125
+ # 2. Patch requests library
126
+ patch_requests()
127
+
128
+ app = Flask(__name__)
129
+
130
+ # 3. Add FlaskRequestIdMiddleware
131
+ FlaskRequestIdMiddleware(app)
132
+
133
+ @app.route('/')
134
+ def hello():
135
+ return 'Hello, World!'
136
+
137
+ # Your application code here...
138
+ ```
139
+
140
+ The Flask middleware also supports the application factory pattern:
141
+
142
+ ```python
143
+ from flask import Flask
144
+ from litewave_logger import setup_logging
145
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
146
+
147
+ middleware = FlaskRequestIdMiddleware()
148
+
149
+ def create_app():
150
+ setup_logging(excluded_endpoints=['/health'])
151
+ app = Flask(__name__)
152
+ middleware.init_app(app)
153
+ return app
154
+ ```
155
+
98
156
  ## How It Works
99
157
 
100
158
  ### Request ID Flow
@@ -8,6 +8,7 @@ This module provides a centralized and consistent logging solution for Litewave
8
8
  - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
9
9
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
10
10
  - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
11
+ - **Flask Integration**: Middleware for Flask to handle `request-id` for incoming HTTP requests and adds it to response headers.
11
12
  - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
12
13
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
13
14
  - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
@@ -40,6 +41,16 @@ To use the `litewave_logger` in your service, follow these steps:
40
41
  app.add_middleware(RequestIdMiddleware)
41
42
  ```
42
43
 
44
+ **Or for Flask applications**: If your service is a Flask application, use the `FlaskRequestIdMiddleware`.
45
+
46
+ ```python
47
+ from flask import Flask
48
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
49
+
50
+ app = Flask(__name__)
51
+ FlaskRequestIdMiddleware(app)
52
+ ```
53
+
43
54
  3. **Patch the `requests` library**: To ensure the `request-id` is propagated to other services, patch the `requests` library.
44
55
 
45
56
  ```python
@@ -55,7 +66,7 @@ To use the `litewave_logger` in your service, follow these steps:
55
66
  import litewave_logger.celery
56
67
  ```
57
68
 
58
- ### Example
69
+ ### FastAPI Example
59
70
 
60
71
  Here's a complete example of how to integrate the `litewave_logger` into a FastAPI application:
61
72
 
@@ -82,6 +93,53 @@ app.add_middleware(RequestIdMiddleware)
82
93
  # Your application code here...
83
94
  ```
84
95
 
96
+ ### Flask Example
97
+
98
+ Here's a complete example of how to integrate the `litewave_logger` into a Flask application:
99
+
100
+ ```python
101
+ from flask import Flask
102
+ from litewave_logger import setup_logging
103
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
104
+ from litewave_logger.requests import patch_requests
105
+
106
+ # Import Celery module to register signal handlers (if using Celery)
107
+ import litewave_logger.celery
108
+
109
+ # 1. Initialize logging (optionally exclude endpoints)
110
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
111
+
112
+ # 2. Patch requests library
113
+ patch_requests()
114
+
115
+ app = Flask(__name__)
116
+
117
+ # 3. Add FlaskRequestIdMiddleware
118
+ FlaskRequestIdMiddleware(app)
119
+
120
+ @app.route('/')
121
+ def hello():
122
+ return 'Hello, World!'
123
+
124
+ # Your application code here...
125
+ ```
126
+
127
+ The Flask middleware also supports the application factory pattern:
128
+
129
+ ```python
130
+ from flask import Flask
131
+ from litewave_logger import setup_logging
132
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
133
+
134
+ middleware = FlaskRequestIdMiddleware()
135
+
136
+ def create_app():
137
+ setup_logging(excluded_endpoints=['/health'])
138
+ app = Flask(__name__)
139
+ middleware.init_app(app)
140
+ return app
141
+ ```
142
+
85
143
  ## How It Works
86
144
 
87
145
  ### Request ID Flow
@@ -78,4 +78,4 @@ def get_excluded_endpoints():
78
78
  """
79
79
  return _excluded_endpoints
80
80
 
81
- __all__ = ["setup_logging", "request_id_var", "RequestIdFilter", "get_excluded_endpoints"]
81
+ __all__ = ["setup_logging", "request_id_var", "RequestIdFilter", "get_excluded_endpoints", "JsonLogFormatter"]
@@ -0,0 +1,116 @@
1
+ import uuid
2
+ import logging
3
+ from flask import Flask, request, g
4
+
5
+ from . import request_id_var, get_excluded_endpoints
6
+
7
+ logger = logging.getLogger(__name__)
8
+ RequestIdHeader = "X-Request-ID"
9
+
10
+
11
+ class FlaskRequestIdMiddleware:
12
+ """
13
+ Flask middleware to handle the request ID and provide comprehensive debugging.
14
+ It checks for a 'X-Request-ID' header in the incoming request.
15
+ If the header is present, its value is used as the request ID.
16
+ If not, a new UUID is generated.
17
+ The request ID is then stored in a context variable.
18
+ Provides detailed logging for incoming requests and outgoing responses.
19
+
20
+ Usage:
21
+ from flask import Flask
22
+ from litewave_logger import setup_logging
23
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
24
+
25
+ app = Flask(__name__)
26
+ setup_logging(excluded_endpoints=['/health'])
27
+ FlaskRequestIdMiddleware(app)
28
+ """
29
+
30
+ def __init__(self, app: Flask = None):
31
+ """
32
+ Initialize the middleware.
33
+
34
+ Args:
35
+ app: Flask application instance. If provided, init_app() is called automatically.
36
+ """
37
+ self.app = app
38
+ if app is not None:
39
+ self.init_app(app)
40
+
41
+ def init_app(self, app: Flask):
42
+ """
43
+ Initialize the middleware with a Flask application.
44
+ Supports Flask's application factory pattern.
45
+
46
+ Args:
47
+ app: Flask application instance.
48
+ """
49
+ app.before_request(self._before_request)
50
+ app.after_request(self._after_request)
51
+
52
+ def _before_request(self):
53
+ """
54
+ Handler called before each request.
55
+ Sets up request ID and logs incoming request.
56
+ """
57
+ # Get or generate request ID
58
+ request_id = request.headers.get(RequestIdHeader)
59
+ if not request_id:
60
+ request_id = str(uuid.uuid4())
61
+
62
+ # Store request ID in Flask's g object for easy access in views
63
+ g.request_id = request_id
64
+
65
+ # Set request ID in context variable (for logging filter)
66
+ request_id_var.set(request_id)
67
+
68
+ # Check if this endpoint should be excluded from logging
69
+ excluded_endpoints = get_excluded_endpoints()
70
+ path = request.path
71
+ g.should_log = path not in excluded_endpoints
72
+
73
+ if g.should_log:
74
+ logger.info("request received", extra={
75
+ "method": request.method,
76
+ "path": path
77
+ })
78
+
79
+ def _after_request(self, response):
80
+ """
81
+ Handler called after each request.
82
+ Adds request ID to response headers and logs response.
83
+
84
+ Args:
85
+ response: Flask response object.
86
+
87
+ Returns:
88
+ Modified response with request ID header.
89
+ """
90
+ # Add request ID to response headers
91
+ request_id = getattr(g, 'request_id', None)
92
+ if request_id:
93
+ response.headers[RequestIdHeader] = request_id
94
+
95
+ # Log response details only if endpoint is not excluded
96
+ should_log = getattr(g, 'should_log', True)
97
+ if should_log:
98
+ logger.info("response sent", extra={
99
+ "status_code": response.status_code
100
+ })
101
+
102
+ return response
103
+
104
+
105
+ def get_request_id():
106
+ """
107
+ Utility function to get the current request ID from Flask's g object.
108
+
109
+ Returns:
110
+ The current request ID or None if not available.
111
+ """
112
+ return getattr(g, 'request_id', None)
113
+
114
+
115
+ __all__ = ["FlaskRequestIdMiddleware", "get_request_id"]
116
+
@@ -31,6 +31,8 @@ class RequestIdMiddleware(BaseHTTPMiddleware):
31
31
  request_id = request.headers.get(RequestIdHeader)
32
32
  if not request_id:
33
33
  request_id = str(uuid.uuid4())
34
+ # Set the request ID in the request headers if not present
35
+ request.scope["headers"].append((RequestIdHeader.lower().encode(), request_id.encode()))
34
36
 
35
37
  # Set request ID in context
36
38
  request_id_var.set(request_id)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: litewave_logger
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: A lightweight logging library with context carry forward
5
5
  Author: Litewave
6
6
  Author-email: Sonu Sudhakaran <sonu@litewave.ai>
@@ -21,6 +21,7 @@ This module provides a centralized and consistent logging solution for Litewave
21
21
  - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
22
22
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
23
23
  - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
+ - **Flask Integration**: Middleware for Flask to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
25
  - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
25
26
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
26
27
  - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
@@ -53,6 +54,16 @@ To use the `litewave_logger` in your service, follow these steps:
53
54
  app.add_middleware(RequestIdMiddleware)
54
55
  ```
55
56
 
57
+ **Or for Flask applications**: If your service is a Flask application, use the `FlaskRequestIdMiddleware`.
58
+
59
+ ```python
60
+ from flask import Flask
61
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
62
+
63
+ app = Flask(__name__)
64
+ FlaskRequestIdMiddleware(app)
65
+ ```
66
+
56
67
  3. **Patch the `requests` library**: To ensure the `request-id` is propagated to other services, patch the `requests` library.
57
68
 
58
69
  ```python
@@ -68,7 +79,7 @@ To use the `litewave_logger` in your service, follow these steps:
68
79
  import litewave_logger.celery
69
80
  ```
70
81
 
71
- ### Example
82
+ ### FastAPI Example
72
83
 
73
84
  Here's a complete example of how to integrate the `litewave_logger` into a FastAPI application:
74
85
 
@@ -95,6 +106,53 @@ app.add_middleware(RequestIdMiddleware)
95
106
  # Your application code here...
96
107
  ```
97
108
 
109
+ ### Flask Example
110
+
111
+ Here's a complete example of how to integrate the `litewave_logger` into a Flask application:
112
+
113
+ ```python
114
+ from flask import Flask
115
+ from litewave_logger import setup_logging
116
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
117
+ from litewave_logger.requests import patch_requests
118
+
119
+ # Import Celery module to register signal handlers (if using Celery)
120
+ import litewave_logger.celery
121
+
122
+ # 1. Initialize logging (optionally exclude endpoints)
123
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
124
+
125
+ # 2. Patch requests library
126
+ patch_requests()
127
+
128
+ app = Flask(__name__)
129
+
130
+ # 3. Add FlaskRequestIdMiddleware
131
+ FlaskRequestIdMiddleware(app)
132
+
133
+ @app.route('/')
134
+ def hello():
135
+ return 'Hello, World!'
136
+
137
+ # Your application code here...
138
+ ```
139
+
140
+ The Flask middleware also supports the application factory pattern:
141
+
142
+ ```python
143
+ from flask import Flask
144
+ from litewave_logger import setup_logging
145
+ from litewave_logger.flask_middleware import FlaskRequestIdMiddleware
146
+
147
+ middleware = FlaskRequestIdMiddleware()
148
+
149
+ def create_app():
150
+ setup_logging(excluded_endpoints=['/health'])
151
+ app = Flask(__name__)
152
+ middleware.init_app(app)
153
+ return app
154
+ ```
155
+
98
156
  ## How It Works
99
157
 
100
158
  ### Request ID Flow
@@ -3,6 +3,7 @@ pyproject.toml
3
3
  setup.py
4
4
  litewave_logger/__init__.py
5
5
  litewave_logger/celery.py
6
+ litewave_logger/flask_middleware.py
6
7
  litewave_logger/middleware.py
7
8
  litewave_logger/requests.py
8
9
  litewave_logger.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "litewave_logger"
3
- version = "0.4.0"
3
+ version = "0.6.0"
4
4
  description = "A lightweight logging library with context carry forward"
5
5
  authors = [{name = "Sonu Sudhakaran", email = "sonu@litewave.ai"}]
6
6
  dependencies = [
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="litewave_logger",
5
- version="0.4.0",
5
+ version="0.6.0",
6
6
  description="A centralized logging module for Litewave services.",
7
7
  author="Litewave",
8
8
  packages=find_packages(),