Skip to content

Commit

Permalink
Merge pull request #1015 from MuckRock/1012-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast authored Jan 16, 2025
2 parents 86a4faf + 4299264 commit 252f17e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ declare global {
};
}
// interface PageState {}
// interface Platform {}
interface Platform {
context: any;
}
}

namespace svelteHTML {
Expand Down
16 changes: 15 additions & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(event, response);
return response;
}

export const handle: Handle = sequence(
Sentry.sentryHandle(),
language,
logRequest,
);
30 changes: 30 additions & 0 deletions src/lib/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// log requests

import type { RequestEvent } from "@sveltejs/kit";

/**
* Log a request inside a Handle function
* @param status
* @param event
*/
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.route.id,
etag,
cache,
].filter(Boolean);

f(...row);
}

0 comments on commit 252f17e

Please sign in to comment.