fix(log): add statusCode in log context
Browse files- src/hooks.server.ts +6 -0
- src/lib/server/logger.ts +1 -0
- src/lib/server/requestContext.ts +2 -0
src/hooks.server.ts
CHANGED
|
@@ -281,6 +281,9 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|
| 281 |
},
|
| 282 |
});
|
| 283 |
|
|
|
|
|
|
|
|
|
|
| 284 |
// Add CSP header to disallow framing if ALLOW_IFRAME is not "true"
|
| 285 |
if (config.ALLOW_IFRAME !== "true") {
|
| 286 |
response.headers.append("Content-Security-Policy", "frame-ancestors 'none';");
|
|
@@ -319,6 +322,9 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|
| 319 |
response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
| 320 |
}
|
| 321 |
}
|
|
|
|
|
|
|
|
|
|
| 322 |
return response;
|
| 323 |
},
|
| 324 |
{ requestId, url: event.url.pathname, ip: getClientAddressSafe(event) }
|
|
|
|
| 281 |
},
|
| 282 |
});
|
| 283 |
|
| 284 |
+
// Update request context with status code
|
| 285 |
+
updateRequestContext({ statusCode: response.status });
|
| 286 |
+
|
| 287 |
// Add CSP header to disallow framing if ALLOW_IFRAME is not "true"
|
| 288 |
if (config.ALLOW_IFRAME !== "true") {
|
| 289 |
response.headers.append("Content-Security-Policy", "frame-ancestors 'none';");
|
|
|
|
| 322 |
response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
| 323 |
}
|
| 324 |
}
|
| 325 |
+
|
| 326 |
+
logger.info("Request completed");
|
| 327 |
+
|
| 328 |
return response;
|
| 329 |
},
|
| 330 |
{ requestId, url: event.url.pathname, ip: getClientAddressSafe(event) }
|
src/lib/server/logger.ts
CHANGED
|
@@ -34,6 +34,7 @@ const baseLogger = pino({
|
|
| 34 |
if (ctx.url) result.url = ctx.url;
|
| 35 |
if (ctx.ip) result.ip = ctx.ip;
|
| 36 |
if (ctx.user) result.user = ctx.user;
|
|
|
|
| 37 |
return result;
|
| 38 |
},
|
| 39 |
});
|
|
|
|
| 34 |
if (ctx.url) result.url = ctx.url;
|
| 35 |
if (ctx.ip) result.ip = ctx.ip;
|
| 36 |
if (ctx.user) result.user = ctx.user;
|
| 37 |
+
if (ctx.statusCode) result.statusCode = String(ctx.statusCode);
|
| 38 |
return result;
|
| 39 |
},
|
| 40 |
});
|
src/lib/server/requestContext.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface RequestContext {
|
|
| 6 |
url?: string;
|
| 7 |
ip?: string;
|
| 8 |
user?: string;
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();
|
|
@@ -23,6 +24,7 @@ export function runWithRequestContext<T>(
|
|
| 23 |
url: context.url,
|
| 24 |
ip: context.ip,
|
| 25 |
user: context.user,
|
|
|
|
| 26 |
};
|
| 27 |
return asyncLocalStorage.run(fullContext, fn);
|
| 28 |
}
|
|
|
|
| 6 |
url?: string;
|
| 7 |
ip?: string;
|
| 8 |
user?: string;
|
| 9 |
+
statusCode?: number;
|
| 10 |
}
|
| 11 |
|
| 12 |
const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();
|
|
|
|
| 24 |
url: context.url,
|
| 25 |
ip: context.ip,
|
| 26 |
user: context.user,
|
| 27 |
+
statusCode: context.statusCode,
|
| 28 |
};
|
| 29 |
return asyncLocalStorage.run(fullContext, fn);
|
| 30 |
}
|