forked from thepeacockproject/Peacock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloggingInterop.ts
More file actions
323 lines (290 loc) · 8.61 KB
/
loggingInterop.ts
File metadata and controls
323 lines (290 loc) · 8.61 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* The Peacock Project - a HITMAN server replacement.
* Copyright (C) 2021-2026 The Peacock Project Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { NextFunction, Request, Response } from "express"
import picocolors from "picocolors"
import winston from "winston"
import "winston-daily-rotate-file"
/**
* Represents the different log levels.
*/
export enum LogLevel {
/**
* For errors. Displays in red.
*/
ERROR = "error",
/**
* For warnings. Displays in yellow.
*/
WARN = "warn",
/**
* For information. Displays in blue.
* This is also the fallback for invalid log level values.
*/
INFO = "info",
/**
* For debugging. Displays in light blue.
*/
DEBUG = "debug",
/**
* For outputting stack traces.
*/
TRACE = "trace",
/**
* For extremely verbose purposes.
*/
SILLY = "silly",
}
/**
* Represents the different internal log categories used by Peacock.
* @internal
*/
export const enum LogCategory {
/**
* Remove the category from the log
*/
NONE = "none",
/**
* Used for logging HTTP request
*/
HTTP = "http",
}
const LOG_LEVEL_NONE = "none"
const LOG_CATEGORY_DEFAULT = LogCategory.NONE
const fileLogLevel = process.env.LOG_LEVEL_FILE || LogLevel.SILLY
const consoleLogLevel = process.env.LOG_LEVEL_CONSOLE || LogLevel.SILLY
const disabledLogCategories =
process.env.LOG_CATEGORY_DISABLED?.split(",") || []
const transports = []
if (fileLogLevel !== LOG_LEVEL_NONE) {
const fileTransport = new winston.transports.DailyRotateFile({
filename: "logs/peacock-%DATE%.json",
datePattern: "YYYYMMDDHHmmss",
frequency: "1d",
maxFiles: process.env.LOG_MAX_FILES,
level: fileLogLevel,
format: winston.format.printf((info) => {
return JSON.stringify(info.data)
}),
})
transports.push(fileTransport)
}
if (consoleLogLevel !== LOG_LEVEL_NONE) {
const consoleTransport = new winston.transports.Console({
level: consoleLogLevel,
format: winston.format.combine(
winston.format((info) => {
if (
// @ts-expect-error todo fix types
!info.data.category ||
// @ts-expect-error todo fix types
!disabledLogCategories.includes(info.data.category)
) {
return info
}
return false
})(),
winston.format.printf((info) => {
// @ts-expect-error todo fix types
if (info.data.stack) {
// @ts-expect-error todo fix types
return `${info.message}\n${info.data.stack}`
}
return info.message as string
}),
),
})
transports.push(consoleTransport)
}
const winstonLogLevel = {}
// @ts-expect-error Type mismatch.
Object.values(LogLevel).forEach((e, i) => (winstonLogLevel[e] = i))
const logger = winston.createLogger({
levels: winstonLogLevel,
transports: transports,
})
/**
* Adds leading zeros to a number so that the length of the string will always
* be the number of places specified.
*
* @param num The number.
* @param places The intended width of the number (character count).
* @example
* zeroPad(5, 2) // -> "05"
*/
const zeroPad = (num: string | number, places: number) =>
String(num).padStart(places, "0")
/**
* Outputs all given arguments as a debug level indented JSON-message to the console.
*
* @param args The values to log.
*/
export function logDebug(...args: unknown[]): void {
log(LogLevel.DEBUG, JSON.stringify(args, undefined, " "))
}
function fixMessage(message: string | unknown | null | undefined): string {
switch (typeof message) {
case "string":
return message
case "object":
return JSON.stringify(message, undefined, 4)
case "undefined":
return "undefined"
case "function":
return "function"
default:
return String(message)
}
}
/**
* Outputs a log message to the console.
*
* @param level The message's level.
* @param data The data to output.
* @param category The message's category.
* @see LogLevel
*
* @function log
*/
export function log(
level: LogLevel,
data: string | unknown,
category: LogCategory | string = LOG_CATEGORY_DEFAULT,
): void {
const message = fixMessage(data)
const now = new Date()
const stampParts: number[] = [
now.getHours(),
now.getMinutes(),
now.getSeconds(),
]
const millis = zeroPad(now.getMilliseconds(), 3)
const timestamp = `${stampParts
.map((part) => zeroPad(part, 2))
.join(":")}:${millis}`
const timestampColored = picocolors.gray(timestamp)
const categoryColored = picocolors.gray(category)
let levelString: string
let levelStringColored: string
let stack = undefined
switch (level) {
case LogLevel.ERROR:
levelString = "Error"
levelStringColored = picocolors.red(levelString)
break
case LogLevel.WARN:
levelString = "Warn"
levelStringColored = picocolors.yellow(levelString)
break
case LogLevel.INFO:
default:
levelString = "Info"
levelStringColored = picocolors.blue(levelString)
break
case LogLevel.DEBUG:
levelString = "Debug"
levelStringColored = picocolors.blueBright(levelString)
break
case LogLevel.TRACE:
levelString = "Trace"
levelStringColored = picocolors.bgYellow(levelString)
stack = new Error("Trace").stack
break
case LogLevel.SILLY:
levelString = "Silly"
levelStringColored = picocolors.bgMagenta(levelString)
break
}
const categoryAndLevel =
category === LogCategory.NONE
? levelStringColored
: `${levelStringColored} | ${categoryColored}`
logger.log(
level,
`[${timestampColored}] [${categoryAndLevel}] ${message}`,
{
data: {
timestamp: timestamp,
category: category,
level: levelString,
message: message,
stack: stack,
},
},
)
}
/**
* Express middleware that logs all requests and their details with the info log level.
*
* @param req The Express request object.
* @param _ The Express response object.
* @param next The Express next function.
* @see LogLevel.INFO
*/
export function loggingMiddleware(
req: Request,
_: Response,
next?: NextFunction,
): void {
log(
LogLevel.INFO,
`${picocolors.green(req.method)} ${picocolors.underline(req.url)}`,
LogCategory.HTTP,
)
next?.()
}
export function requestLoggingMiddleware(
req: Request,
res: Response,
next?: NextFunction,
): void {
res.once("finish", () => {
const debug = {
method: req.method,
url: req.url,
body: req.body,
statusCode: res.statusCode,
statusMessage: res.statusMessage,
}
log(LogLevel.DEBUG, JSON.stringify(debug), LogCategory.HTTP)
})
next?.()
}
export function errorLoggingMiddleware(
err: Error,
req: Request,
_: Response,
next?: NextFunction,
): void {
const debug = {
method: req.method,
url: req.url,
body: req.body,
error: `${err.name} - ${err.message} - ${
err.cause || "Unknown cause"
}\n${err.stack || "No stack"}`,
}
log(
LogLevel.ERROR,
`${picocolors.green(req.method)} ${picocolors.underline(
req.url,
)} gave an unexpected error! Please see log for details.`,
LogCategory.HTTP,
)
log(LogLevel.DEBUG, JSON.stringify(debug), LogCategory.HTTP)
next?.()
}