-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.ts
172 lines (156 loc) Β· 4.21 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2018-2024 the oak authors. All rights reserved.
import {
BaseHandler,
type BaseHandlerOptions,
ConsoleHandler,
type FormatterFunction,
getLogger as gl,
type LevelName,
type Logger,
type LoggerConfig,
RotatingFileHandler,
setup,
} from "@std/log";
import { isBun, isNode } from "./utils.ts";
export { type Logger } from "@std/log";
/**
* Options which can be set when configuring file logging on the logger.
*/
export interface FileLoggerOptions {
/**
* The log level to log at. The default is `"INFO"`.
*/
level?: LevelName;
/**
* The maximum number of log files to keep. The default is `5`.
*/
maxBackupCount?: number;
/**
* The maximum size of a log file before it is rotated in bytes. The default
* is 10MB.
*/
maxBytes?: number;
/**
* The path to the log file.
*/
filename: string;
}
/**
* Options which can be set when configuring the logger when creating a new
* router.
*/
export interface LoggerOptions {
/**
* Log events to the console. If `true`, log at the "INFO" level. If an
* object, the `level` can be specified.
*/
console?: boolean | { level: LevelName };
/**
* Log events to a rotating log file. The value should be an object with the
* `path` to the log file and optionally the `level` to log at. If `level` is
* not specified, the default is `"INFO"`.
*/
file?: FileLoggerOptions;
/**
* Log events to a stream. The value should be an object with the `stream` to
* pipe the log events to and optionally the `level` to log at. If `level` is
* not specified, the default is `"info"`.
*/
stream?: { level?: LevelName; stream: WritableStream };
}
let inspect: (value: unknown) => string;
if (typeof globalThis?.Deno?.inspect === "function") {
inspect = Deno.inspect;
} else {
inspect = (value) => JSON.stringify(value);
if (isNode() || isBun()) {
import("node:util").then(({ inspect: nodeInspect }) => {
inspect = nodeInspect;
});
}
}
const formatter: FormatterFunction = (
{ datetime, levelName, loggerName, msg, args },
) =>
`${datetime.toISOString()} [${levelName}] ${loggerName}: ${msg} ${
args.map((arg) => inspect(arg)).join(" ")
}`;
const encoder = new TextEncoder();
class StreamHandler extends BaseHandler {
#writer: WritableStreamDefaultWriter<Uint8Array>;
constructor(
levelName: LevelName,
stream: WritableStream<Uint8Array>,
options?: BaseHandlerOptions,
) {
super(levelName, options);
this.#writer = stream.getWriter();
}
log(msg: string): void {
this.#writer.write(encoder.encode(msg + "\n"));
}
override destroy(): void {
this.#writer.close();
}
}
const mods = [
"acorn.context",
"acorn.request_event_cfw",
"acorn.request_server_bun",
"acorn.request_server_deno",
"acorn.request_server_node",
"acorn.route",
"acorn.router",
"acorn.schema",
"acorn.status_route",
] as const;
type Loggers = typeof mods[number];
export function getLogger(mod: Loggers): Logger {
return gl(mod);
}
export function configure(options?: LoggerOptions): void {
const config = {
handlers: {} as { [key: string]: BaseHandler },
loggers: {} as { [key: string]: LoggerConfig },
};
const handlers: string[] = [];
if (options) {
if (options.console) {
config.handlers.console = new ConsoleHandler(
typeof options.console === "object" ? options.console.level : "INFO",
{ formatter },
);
handlers.push("console");
}
if (options.file) {
const {
filename,
maxBackupCount = 5,
maxBytes = 1024 * 1024 * 10,
level = "INFO",
} = options.file;
config.handlers.file = new RotatingFileHandler(level, {
filename,
maxBackupCount,
maxBytes,
formatter,
});
handlers.push("file");
}
if (options.stream) {
config.handlers.stream = new StreamHandler(
options.stream.level ?? "INFO",
options.stream.stream,
{ formatter },
);
handlers.push("stream");
}
} else {
config.handlers.console = new ConsoleHandler("WARN", { formatter });
handlers.push("console");
}
for (const mod of mods) {
config.loggers[mod] = { level: "DEBUG", handlers };
}
setup(config);
}