-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
97 lines (88 loc) · 2.51 KB
/
mod.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { bold, cyan, green, red, yellow } from "./deps.ts";
import { format } from "./deps.ts";
import { Context, Middleware } from "./deps.ts";
import { defaultConfig, LoggerConfig } from "./config.ts";
import { Data } from "./data.ts";
/// Creates a New Logger Middleware with the given config.
export function New(config: Partial<LoggerConfig> | undefined): Middleware {
const conf: LoggerConfig = { ...defaultConfig, ...config };
return createMw(conf);
}
function createMw(config: LoggerConfig): Middleware {
const te = new TextEncoder();
const writeToWriter = (writer: Deno.Writer, buf: Uint8Array) => {
writer.write(buf);
};
return async (ctx, next) => {
await next();
const data = extractData(ctx, config);
const buffer = formatToBuffer(data, config, te);
writeToWriter(config.output, buffer);
};
}
function extractData(
{ request, response }: Context,
{ timeFormat }: LoggerConfig,
): Data {
return {
host: request.url.host,
ip: request.ip,
method: request.method,
path: request.url.pathname,
status: response.status,
timestamp: format(new Date(), timeFormat),
};
}
function formatToBuffer(
data: Data,
config: LoggerConfig,
te: TextEncoder,
): Uint8Array {
if (config.fmt === "json") {
return te.encode(JSON.stringify(data));
} else {
return te.encode(formatToText(data, config));
}
}
function formatToText(
data: Data,
{ enableColors, format }: LoggerConfig,
): string {
if (enableColors) {
const method = bold(cyan(data.method));
const path = red(data.path);
const time = data.timestamp;
const status = colorStatus(data.status);
const ip = bold(data.ip);
const host = bold(data.host);
return format
.replace("${method}", method)
.replace("${path}", path)
.replace("${time}", time)
.replace("${status}", status)
.replace("${ip}", ip)
.replace("${host}", host);
} else {
return format
.replace("${method}", data.method)
.replace("${path}", data.path)
.replace("${time}", data.timestamp)
.replace("${status}", data.status.toString())
.replace("${ip}", data.ip)
.replace("${host}", data.host);
}
}
function colorStatus(status: number): string {
if (status >= 500) {
return red(status.toString());
} else if (status >= 400) {
return yellow(status.toString());
} else if (status >= 300) {
return green(status.toString());
} else {
return green(status.toString());
}
}
export function Default(): Middleware {
return New(undefined);
}