-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1015 from MuckRock/1012-logging
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |