From 0f504870ba77165be21651c14ef44b88f97d4750 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Wed, 15 Jan 2025 17:55:34 -0500 Subject: [PATCH 1/2] Let's log things --- src/app.d.ts | 4 +++- src/hooks.server.ts | 16 +++++++++++++++- src/lib/utils/logging.ts | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/lib/utils/logging.ts diff --git a/src/app.d.ts b/src/app.d.ts index df09b3362..38fb2b2ca 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -12,7 +12,9 @@ declare global { }; } // interface PageState {} - // interface Platform {} + interface Platform { + context: any; + } } namespace svelteHTML { diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 7f0e39b11..934299824 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -8,6 +8,7 @@ import * as Sentry from "@sentry/sveltekit"; import { locale } from "svelte-i18n"; import { DC_BASE } from "./config/config.js"; +import { log } from "$lib/utils/logging"; Sentry.init({ dsn: env.SENTRY_DSN, @@ -56,4 +57,17 @@ async function language({ event, resolve }) { }); } -export const handle: Handle = sequence(Sentry.sentryHandle(), language); +/** @type {import('@sveltejs/kit').Handle} */ +async function logRequest({ event, resolve }) { + const response = await resolve(event); + + // logging happens after the response is generated + log(response.status, event); + return response; +} + +export const handle: Handle = sequence( + Sentry.sentryHandle(), + language, + logRequest, +); diff --git a/src/lib/utils/logging.ts b/src/lib/utils/logging.ts new file mode 100644 index 000000000..4576dd7bf --- /dev/null +++ b/src/lib/utils/logging.ts @@ -0,0 +1,19 @@ +// log requests + +import type { RequestEvent } from "@sveltejs/kit"; + +/** + * Log a request inside a Handle function + * @param status + * @param event + */ +export function log(status: number, event: RequestEvent): void { + // be loud about errors + const f = status >= 400 ? console.warn : console.info; + + const { method, url } = event.request; + + const row = [new Date(), method, url, status, event.platform?.context ?? ""]; + + f(...row); +} From 4299264d849dc5c9fa5b316f797738ebb5b7cc50 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Thu, 16 Jan 2025 11:25:45 -0500 Subject: [PATCH 2/2] Route ID, etag, cache control --- src/hooks.server.ts | 2 +- src/lib/utils/logging.ts | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 934299824..072e75289 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -62,7 +62,7 @@ async function logRequest({ event, resolve }) { const response = await resolve(event); // logging happens after the response is generated - log(response.status, event); + log(event, response); return response; } diff --git a/src/lib/utils/logging.ts b/src/lib/utils/logging.ts index 4576dd7bf..a52ff09b2 100644 --- a/src/lib/utils/logging.ts +++ b/src/lib/utils/logging.ts @@ -7,13 +7,24 @@ import type { RequestEvent } from "@sveltejs/kit"; * @param status * @param event */ -export function log(status: number, event: RequestEvent): void { +export function log(event: RequestEvent, response: Response): void { // be loud about errors + const status = response.status; const f = status >= 400 ? console.warn : console.info; const { method, url } = event.request; + const cache = response.headers.get("cache-control") ?? ""; + const etag = response.headers.get("etag") ?? ""; - const row = [new Date(), method, url, status, event.platform?.context ?? ""]; + const row = [ + new Date(), + method, + url, + status, + event.route.id, + etag, + cache, + ].filter(Boolean); f(...row); }