litewave-logger 0.2.0__tar.gz → 0.3.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.2.0
3
+ Version: 0.3.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>
@@ -18,10 +18,12 @@ This module provides a centralized and consistent logging solution for Litewave
18
18
  ## Features
19
19
 
20
20
  - **Centralized Logging**: A single module to configure and manage logging across all services.
21
+ - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
21
22
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
22
- - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests.
23
- - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks.
23
+ - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
+ - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
24
25
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
26
+ - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
25
27
 
26
28
  ## Installation
27
29
 
@@ -37,7 +39,8 @@ To use the `litewave_logger` in your service, follow these steps:
37
39
  ```python
38
40
  from litewave_logger import setup_logging
39
41
 
40
- setup_logging()
42
+ # Optionally exclude endpoints from logging (e.g., health checks, metrics)
43
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
41
44
  ```
42
45
 
43
46
  2. **Add the FastAPI middleware**: If your service is a FastAPI application, add the `RequestIdMiddleware` to your FastAPI app.
@@ -58,10 +61,11 @@ To use the `litewave_logger` in your service, follow these steps:
58
61
  patch_requests()
59
62
  ```
60
63
 
61
- 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery signal handlers to ensure they are registered. You don't need to call them directly.
64
+ 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery module to ensure the signal handlers are registered. The signal handlers are automatically connected via decorators, so you don't need to call them directly.
62
65
 
63
66
  ```python
64
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
67
+ # Just import the module - signal handlers are automatically registered
68
+ import litewave_logger.celery
65
69
  ```
66
70
 
67
71
  ### Example
@@ -74,11 +78,11 @@ from litewave_logger import setup_logging
74
78
  from litewave_logger.middleware import RequestIdMiddleware
75
79
  from litewave_logger.requests import patch_requests
76
80
 
77
- # These imports are needed to register the signal handlers
78
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
81
+ # Import Celery module to register signal handlers (if using Celery)
82
+ import litewave_logger.celery
79
83
 
80
- # 1. Initialize logging
81
- setup_logging()
84
+ # 1. Initialize logging (optionally exclude endpoints)
85
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
82
86
 
83
87
  # 2. Patch requests library
84
88
  patch_requests()
@@ -90,3 +94,31 @@ app.add_middleware(RequestIdMiddleware)
90
94
 
91
95
  # Your application code here...
92
96
  ```
97
+
98
+ ## How It Works
99
+
100
+ ### Request ID Flow
101
+
102
+ 1. **Incoming HTTP Request**: The `RequestIdMiddleware` checks for an `X-Request-ID` header. If present, it uses that value; otherwise, it generates a new UUID.
103
+ 2. **Context Variable**: The request ID is stored in a context variable (`request_id_var`) that is automatically maintained across async operations.
104
+ 3. **Logging**: All log messages automatically include the request ID via the `RequestIdFilter`.
105
+ 4. **Outgoing Requests**: When using the `requests` library, the request ID is automatically injected as the `X-Request-ID` header.
106
+ 5. **Response Headers**: The request ID is added to the response headers as `X-Request-ID`.
107
+ 6. **Celery Tasks**: When a Celery task is published, the request ID is automatically included in the task headers and propagated to the worker process.
108
+
109
+ ### Log Format
110
+
111
+ All logs are formatted as JSON with the following structure:
112
+
113
+ ```json
114
+ {
115
+ "timestamp": "2024-01-01 12:00:00",
116
+ "level": "INFO",
117
+ "request_id": "550e8400-e29b-41d4-a716-446655440000",
118
+ "path": "/api/users",
119
+ "method": "GET",
120
+ "message": "request received",
121
+ "status_code": 200,
122
+ "error": null
123
+ }
124
+ ```
@@ -5,10 +5,12 @@ This module provides a centralized and consistent logging solution for Litewave
5
5
  ## Features
6
6
 
7
7
  - **Centralized Logging**: A single module to configure and manage logging across all services.
8
+ - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
8
9
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
9
- - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests.
10
- - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks.
10
+ - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
11
+ - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
11
12
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
13
+ - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
12
14
 
13
15
  ## Installation
14
16
 
@@ -24,7 +26,8 @@ To use the `litewave_logger` in your service, follow these steps:
24
26
  ```python
25
27
  from litewave_logger import setup_logging
26
28
 
27
- setup_logging()
29
+ # Optionally exclude endpoints from logging (e.g., health checks, metrics)
30
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
28
31
  ```
29
32
 
30
33
  2. **Add the FastAPI middleware**: If your service is a FastAPI application, add the `RequestIdMiddleware` to your FastAPI app.
@@ -45,10 +48,11 @@ To use the `litewave_logger` in your service, follow these steps:
45
48
  patch_requests()
46
49
  ```
47
50
 
48
- 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery signal handlers to ensure they are registered. You don't need to call them directly.
51
+ 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery module to ensure the signal handlers are registered. The signal handlers are automatically connected via decorators, so you don't need to call them directly.
49
52
 
50
53
  ```python
51
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
54
+ # Just import the module - signal handlers are automatically registered
55
+ import litewave_logger.celery
52
56
  ```
53
57
 
54
58
  ### Example
@@ -61,11 +65,11 @@ from litewave_logger import setup_logging
61
65
  from litewave_logger.middleware import RequestIdMiddleware
62
66
  from litewave_logger.requests import patch_requests
63
67
 
64
- # These imports are needed to register the signal handlers
65
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
68
+ # Import Celery module to register signal handlers (if using Celery)
69
+ import litewave_logger.celery
66
70
 
67
- # 1. Initialize logging
68
- setup_logging()
71
+ # 1. Initialize logging (optionally exclude endpoints)
72
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
69
73
 
70
74
  # 2. Patch requests library
71
75
  patch_requests()
@@ -77,3 +81,31 @@ app.add_middleware(RequestIdMiddleware)
77
81
 
78
82
  # Your application code here...
79
83
  ```
84
+
85
+ ## How It Works
86
+
87
+ ### Request ID Flow
88
+
89
+ 1. **Incoming HTTP Request**: The `RequestIdMiddleware` checks for an `X-Request-ID` header. If present, it uses that value; otherwise, it generates a new UUID.
90
+ 2. **Context Variable**: The request ID is stored in a context variable (`request_id_var`) that is automatically maintained across async operations.
91
+ 3. **Logging**: All log messages automatically include the request ID via the `RequestIdFilter`.
92
+ 4. **Outgoing Requests**: When using the `requests` library, the request ID is automatically injected as the `X-Request-ID` header.
93
+ 5. **Response Headers**: The request ID is added to the response headers as `X-Request-ID`.
94
+ 6. **Celery Tasks**: When a Celery task is published, the request ID is automatically included in the task headers and propagated to the worker process.
95
+
96
+ ### Log Format
97
+
98
+ All logs are formatted as JSON with the following structure:
99
+
100
+ ```json
101
+ {
102
+ "timestamp": "2024-01-01 12:00:00",
103
+ "level": "INFO",
104
+ "request_id": "550e8400-e29b-41d4-a716-446655440000",
105
+ "path": "/api/users",
106
+ "method": "GET",
107
+ "message": "request received",
108
+ "status_code": 200,
109
+ "error": null
110
+ }
111
+ ```
@@ -8,6 +8,9 @@ from contextvars import ContextVar
8
8
  # Magically, this is maintained across multiple requests without it getting mixed up.
9
9
  request_id_var = ContextVar("request_id", default=None)
10
10
 
11
+ # List of endpoints that should not be logged
12
+ _excluded_endpoints = []
13
+
11
14
 
12
15
  class RequestIdFilter(logging.Filter):
13
16
  """
@@ -35,11 +38,20 @@ class JsonLogFormatter(logging.Formatter):
35
38
  }
36
39
  return json.dumps(log_object)
37
40
 
38
- def setup_logging():
41
+ def setup_logging(excluded_endpoints=[]):
39
42
  """
40
43
  Configures the logging for the application.
41
44
  Uses a JSON formatter and adds the RequestIdFilter.
45
+
46
+ Args:
47
+ excluded_endpoints: List of endpoint paths that should not be logged.
48
+ For example: ['/health', '/metrics']
49
+ Defaults to None (empty list).
42
50
  """
51
+ global _excluded_endpoints
52
+
53
+ _excluded_endpoints = excluded_endpoints
54
+
43
55
  logger = logging.getLogger()
44
56
  logger.setLevel(logging.INFO)
45
57
 
@@ -57,4 +69,10 @@ def setup_logging():
57
69
 
58
70
  return logger
59
71
 
60
- __all__ = ["setup_logging", "request_id_var", "RequestIdFilter"]
72
+ def get_excluded_endpoints():
73
+ """
74
+ Returns the list of excluded endpoints.
75
+ """
76
+ return _excluded_endpoints
77
+
78
+ __all__ = ["setup_logging", "request_id_var", "RequestIdFilter", "get_excluded_endpoints"]
@@ -5,7 +5,7 @@ from datetime import datetime
5
5
  from starlette.middleware.base import BaseHTTPMiddleware
6
6
  from starlette.requests import Request
7
7
 
8
- from . import request_id_var
8
+ from . import request_id_var, get_excluded_endpoints
9
9
 
10
10
  logger = logging.getLogger(__name__)
11
11
  RequestIdHeader = "X-Request-ID"
@@ -35,7 +35,12 @@ class RequestIdMiddleware(BaseHTTPMiddleware):
35
35
  # Set request ID in context
36
36
  request_id_var.set(request_id)
37
37
 
38
- logger.info("request received", extra={"method": method, "path": path})
38
+ # Check if this endpoint should be excluded from logging
39
+ excluded_endpoints = get_excluded_endpoints()
40
+ should_log = path not in excluded_endpoints
41
+
42
+ if should_log:
43
+ logger.info("request received", extra={"method": method, "path": path})
39
44
 
40
45
  try:
41
46
  # Process the request
@@ -44,13 +49,13 @@ class RequestIdMiddleware(BaseHTTPMiddleware):
44
49
  # Add request ID to response headers
45
50
  response.headers[RequestIdHeader] = request_id
46
51
 
47
- # Log response details
48
- logger.info("response sent", extra={"status_code": response.status_code})
52
+ # Log response details only if endpoint is not excluded
53
+ if should_log:
54
+ logger.info("response sent", extra={"status_code": response.status_code})
49
55
 
50
56
  return response
51
57
 
52
58
  except Exception as e:
53
- # Log error details
54
59
  logger.error("request failed", extra={"method": method, "path": path, "error": str(e)})
55
60
 
56
61
  # Re-raise the exception
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: litewave_logger
3
- Version: 0.2.0
3
+ Version: 0.3.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>
@@ -18,10 +18,12 @@ This module provides a centralized and consistent logging solution for Litewave
18
18
  ## Features
19
19
 
20
20
  - **Centralized Logging**: A single module to configure and manage logging across all services.
21
+ - **JSON Logging**: All logs are formatted as JSON for easy parsing and integration with log aggregation systems.
21
22
  - **Request ID Propagation**: Automatically injects a `request-id` into all log messages.
22
- - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests.
23
- - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks.
23
+ - **FastAPI Integration**: Middleware for FastAPI to handle `request-id` for incoming HTTP requests and adds it to response headers.
24
+ - **Celery Integration**: Signal handlers to propagate the `request-id` to Celery tasks automatically.
24
25
  - **Requests Library Patching**: Automatically injects the `request-id` into outgoing HTTP requests made with the `requests` library.
26
+ - **Endpoint Exclusion**: Configure endpoints that should not be logged (e.g., health checks, metrics).
25
27
 
26
28
  ## Installation
27
29
 
@@ -37,7 +39,8 @@ To use the `litewave_logger` in your service, follow these steps:
37
39
  ```python
38
40
  from litewave_logger import setup_logging
39
41
 
40
- setup_logging()
42
+ # Optionally exclude endpoints from logging (e.g., health checks, metrics)
43
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
41
44
  ```
42
45
 
43
46
  2. **Add the FastAPI middleware**: If your service is a FastAPI application, add the `RequestIdMiddleware` to your FastAPI app.
@@ -58,10 +61,11 @@ To use the `litewave_logger` in your service, follow these steps:
58
61
  patch_requests()
59
62
  ```
60
63
 
61
- 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery signal handlers to ensure they are registered. You don't need to call them directly.
64
+ 4. **Connect Celery signals**: If your service uses Celery, you need to import the Celery module to ensure the signal handlers are registered. The signal handlers are automatically connected via decorators, so you don't need to call them directly.
62
65
 
63
66
  ```python
64
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
67
+ # Just import the module - signal handlers are automatically registered
68
+ import litewave_logger.celery
65
69
  ```
66
70
 
67
71
  ### Example
@@ -74,11 +78,11 @@ from litewave_logger import setup_logging
74
78
  from litewave_logger.middleware import RequestIdMiddleware
75
79
  from litewave_logger.requests import patch_requests
76
80
 
77
- # These imports are needed to register the signal handlers
78
- from litewave_logger.celery import propagate_request_id_to_celery, clear_request_id_after_celery
81
+ # Import Celery module to register signal handlers (if using Celery)
82
+ import litewave_logger.celery
79
83
 
80
- # 1. Initialize logging
81
- setup_logging()
84
+ # 1. Initialize logging (optionally exclude endpoints)
85
+ setup_logging(excluded_endpoints=['/health', '/metrics'])
82
86
 
83
87
  # 2. Patch requests library
84
88
  patch_requests()
@@ -90,3 +94,31 @@ app.add_middleware(RequestIdMiddleware)
90
94
 
91
95
  # Your application code here...
92
96
  ```
97
+
98
+ ## How It Works
99
+
100
+ ### Request ID Flow
101
+
102
+ 1. **Incoming HTTP Request**: The `RequestIdMiddleware` checks for an `X-Request-ID` header. If present, it uses that value; otherwise, it generates a new UUID.
103
+ 2. **Context Variable**: The request ID is stored in a context variable (`request_id_var`) that is automatically maintained across async operations.
104
+ 3. **Logging**: All log messages automatically include the request ID via the `RequestIdFilter`.
105
+ 4. **Outgoing Requests**: When using the `requests` library, the request ID is automatically injected as the `X-Request-ID` header.
106
+ 5. **Response Headers**: The request ID is added to the response headers as `X-Request-ID`.
107
+ 6. **Celery Tasks**: When a Celery task is published, the request ID is automatically included in the task headers and propagated to the worker process.
108
+
109
+ ### Log Format
110
+
111
+ All logs are formatted as JSON with the following structure:
112
+
113
+ ```json
114
+ {
115
+ "timestamp": "2024-01-01 12:00:00",
116
+ "level": "INFO",
117
+ "request_id": "550e8400-e29b-41d4-a716-446655440000",
118
+ "path": "/api/users",
119
+ "method": "GET",
120
+ "message": "request received",
121
+ "status_code": 200,
122
+ "error": null
123
+ }
124
+ ```
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "litewave_logger"
3
- version = "0.2.0"
3
+ version = "0.3.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.2.0",
5
+ version="0.3.0",
6
6
  description="A centralized logging module for Litewave services.",
7
7
  author="Litewave",
8
8
  packages=find_packages(),