-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
35 lines (31 loc) · 1.01 KB
/
Copy pathlogger.js
File metadata and controls
35 lines (31 loc) · 1.01 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
const pino = require('pino');
// 默认配置
const pinoConfig = {
level: process.env.LOG_LEVEL || 'info',
formatters: {
level: (label) => ({ level: label.toUpperCase() }),
},
timestamp: pino.stdTimeFunctions.isoTime,
};
// 在非生产环境中,使用 pino-pretty 美化输出
if (process.env.NODE_ENV !== 'production') {
pinoConfig.transport = {
target: 'pino-pretty',
options: {
colorize: true, // 开启颜色
translateTime: 'SYS:yyyy-mm-dd HH:MM:ss', // 时间格式化
ignore: 'pid,hostname,agentId', // 忽略不必要的字段,让日志更简洁
messageFormat: '[{jobId}] {msg}', // 自定义消息格式
},
};
}
/**
* 创建一个结构化日志记录器。
* 在开发环境中,日志会被 pino-pretty 美化。
* 在生产环境中,输出标准的 JSON 日志。
* @example
* logger.info({ userId: 123 }, "用户已登录");
* logger.error({ err, requestId }, "发生了一个错误");
*/
const logger = pino(pinoConfig);
module.exports = logger;