-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
logger.ts
36 lines (32 loc) 路 1.04 KB
/
logger.ts
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
30
31
32
33
34
35
36
import { brightGreen, dim, red } from "../deps/colors.ts";
import type { Middleware } from "../core.ts";
/** Log the request/responses */
export default function log(): Middleware {
return async (request, next) => {
try {
const response = await next(request);
const url = new URL(request.url);
const pathname = decodeURIComponent(url.pathname);
const { status } = response;
if (status === 404 || status === 500) {
console.log(`${red(status.toString())} ${pathname}`);
} else if (status === 200) {
console.log(`${brightGreen(status.toString())} ${pathname}`);
} else if (status === 301 || status === 302) {
console.log(
`${dim(status.toString())} ${pathname} => ${
response.headers?.get("location")
}`,
);
} else {
console.log(`${dim(status.toString())} ${pathname}`);
}
return response;
} catch (cause) {
return new Response(
`Error: ${cause.toString()}`,
{ status: 500 },
);
}
};
}