Skip to content

Commit fc94896

Browse files
committed
tech: improve grafana logging
1 parent 2c37975 commit fc94896

File tree

4 files changed

+46
-39
lines changed

4 files changed

+46
-39
lines changed

aigle/settings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from core.utils.parsing import strtobool
1818

19+
1920
DEPLOYMENT_DATETIME = datetime.now()
2021

2122
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -129,6 +130,22 @@
129130
},
130131
}
131132

133+
if ENVIRONMENT in ["production", "preprod"]:
134+
LOGGING["handlers"]["scaleway_loki"] = {
135+
"class": "logging_loki.LokiHandler",
136+
"url": os.environ.get("SCW_COCKPIT_URL"),
137+
"tags": {
138+
"job": "django_api",
139+
"environment": ENVIRONMENT,
140+
},
141+
"auth": (
142+
os.environ.get("SCW_SECRET_KEY"),
143+
os.environ.get("SCW_COCKPIT_TOKEN_SECRET_KEY"),
144+
),
145+
"version": "1",
146+
}
147+
148+
132149
ALLOWED_HOSTS = []
133150

134151
if os.environ.get("ALLOWED_HOSTS"):

core/middlewares/logs.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import logging
21
import time
32
import uuid
43

5-
logger = logging.getLogger("aigle") # Replace 'myapp' with your app name
4+
from core.utils.logs_helpers import log_api_call
65

76

87
class RequestLoggingMiddleware:
@@ -14,29 +13,20 @@ def __call__(self, request):
1413
request.id = str(uuid.uuid4())
1514
start_time = time.time()
1615

17-
# Log incoming request
18-
logger.info(
19-
f"Request {request.method} {request.path}",
20-
extra={
21-
"request_id": request.id,
22-
"method": request.method,
23-
"path": request.path,
24-
"user": str(request.user) if hasattr(request, "user") else "Anonymous",
25-
"ip": self.get_client_ip(request),
26-
},
27-
)
28-
2916
response = self.get_response(request)
3017

3118
# Log response
3219
duration = time.time() - start_time
33-
logger.info(
34-
f"Response {response.status_code}",
35-
extra={
36-
"request_id": request.id,
37-
"status_code": response.status_code,
38-
"duration_ms": round(duration * 1000, 2),
39-
},
20+
21+
# Log incoming request
22+
log_api_call(
23+
endpoint=request.path,
24+
method=request.method,
25+
user=str(request.user) if hasattr(request, "user") else "Anonymous",
26+
ip=str(self.get_client_ip(request)),
27+
request_id=request.id,
28+
duration_ms=round(duration * 1000, 2),
29+
status_code=response.status_code,
4030
)
4131

4232
return response

core/utils/logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def setup_scaleway_logger():
1212
# Only use Scaleway logging in production/staging
1313
if environment in ["production", "staging"]:
1414
handler = logging_loki.LokiHandler(
15-
url="https://ad795441-15db-4609-a53f-30ddf578f82d.logs.cockpit.fr-par.scw.cloud",
15+
url=os.environ.get("SCW_COCKPIT_URL"),
1616
tags={"job": "django_api", "environment": environment},
1717
auth=(
1818
os.environ.get("SCW_SECRET_KEY"),
19-
os.environ.get("COCKPIT_TOKEN_SECRET_KEY"),
19+
os.environ.get("SCW_COCKPIT_TOKEN_SECRET_KEY"),
2020
),
2121
version="1",
2222
)

core/utils/logs_helpers.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import logging
2+
from typing import Optional
23

34

45
def get_app_logger():
56
return logging.getLogger("aigle")
67

78

8-
def log_user_action(user, action, **kwargs):
9-
logger = get_app_logger()
10-
logger.info(
11-
f"User action: {action}",
12-
extra={
13-
"user_id": user.id if hasattr(user, "id") else None,
14-
"username": str(user),
15-
"action": action,
16-
**kwargs,
17-
},
18-
)
19-
20-
21-
def log_api_call(endpoint, method, status_code, duration=None, **kwargs):
9+
def log_api_call(
10+
endpoint: str,
11+
method: str,
12+
user: str,
13+
ip: str,
14+
request_id: str,
15+
status_code: str,
16+
duration_ms: Optional[str] = None,
17+
**kwargs,
18+
):
2219
"""Log API calls"""
2320
logger = get_app_logger()
2421
logger.info(
2522
f"API call: {method} {endpoint}",
2623
extra={
2724
"endpoint": endpoint,
2825
"method": method,
26+
"user": user,
27+
"ip": ip,
28+
"request_id": request_id,
2929
"status_code": status_code,
30-
"duration_ms": duration,
30+
"duration_ms": duration_ms,
3131
**kwargs,
3232
},
3333
)
3434

3535

36-
def log_command_event(command_name, info, **kwargs):
36+
def log_command_event(command_name: str, info: str, **kwargs):
3737
logger = get_app_logger()
3838
logger.info(
3939
f"Command event: {command_name}",

0 commit comments

Comments
 (0)