-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
29 lines (23 loc) · 799 Bytes
/
logger.ts
File metadata and controls
29 lines (23 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { ulid } from "@std/ulid";
const EXPIRE_LOGS_DAYS = 90;
const logObject = async (now: Date, req: Request) => {
const ts = Math.floor(now.getTime() / 1000);
return {
method: req.method,
url: req.url,
redirect: req.redirect,
bodyUsed: req.bodyUsed,
...{ ts: ts },
headers: Object.fromEntries(req.headers.entries()),
...(req.body ? { body: await req.text() } : {}),
};
};
const log = async (request: Request, additionalData) => {
const kv = await Deno.openKv();
const now = new Date();
const logRecord = { ...(await logObject(now, request)), ...additionalData };
return await kv.set(["logs", now.getFullYear(), now.getMonth() + 1, now.getDate(), ulid()], logRecord, {
expireIn: 1000 * 60 * 60 * 24 * EXPIRE_LOGS_DAYS,
});
};
export { log };