-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdebug-logger.ts
37 lines (34 loc) · 1.41 KB
/
debug-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
import chalk from 'chalk'
import fs from 'fs'
/**
*
* @description DEBUG 模式下打印日志并写入日志文件
* @param req 请求参数
* @param resp 响应内容
* @param {Date} startTime 请求时间
* @param {Date} endTime 响应时间
* @param writeToLocal 是否写入日志文件,默认写入
* @returns
*/
export function debugLogger(req: unknown, resp: unknown, startTime: Date, endTime: Date, writeToLocal = true): void {
if (process.env.NODE_ENV !== 'DEBUG') {
return
}
const cost = endTime.valueOf() - startTime.valueOf()
const startTimeFormatted = startTime.toISOString()
const endTimeFormatted = endTime.toISOString()
console.log(chalk.underline('\n[DEBUG]', startTimeFormatted))
console.log(chalk.cyan(JSON.stringify(req)), '\n')
console.log(chalk.underline('[DEBUG]', endTimeFormatted))
console.log(chalk.magenta(JSON.stringify(resp)), '\n')
if (writeToLocal) {
const filePath = `${process.cwd()}/cloudbase-cli.debug.log`
const logContent = `\n{start:${startTimeFormatted}, req: ${JSON.stringify(req)}, end:${endTimeFormatted}, resp: ${JSON.stringify(resp)}, cost: ${cost}}`
// 异步追加写入日志
fs.appendFile(filePath, logContent, (err) => {
if (err) {
console.error(chalk.red(`\n写入日志失败:${JSON.stringify(err)}`))
}
})
}
}