Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let's log things #1015

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading