feat: 添加pino用于格式化日志

This commit is contained in:
2025-10-21 09:01:50 +00:00
parent 19aa6886bd
commit 318f62eb47
3 changed files with 299 additions and 5 deletions

29
src/logger.ts Normal file
View File

@ -0,0 +1,29 @@
import pino from 'pino';
/**
* 创建一个带命名空间的 logger 实例
*
* @param scope 当前模块的命名空间
* @returns pino Logger 实例
*/
export function createLogger(scope: string = 'directus-extension') {
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'HH:MM:ss.l',
ignore: 'pid,hostname',
messageFormat: `[${scope}] {levelLabel}: {msg}`,
},
},
});
return {
info: (msg: string, ...args: unknown[]) => logger.info(msg, ...args),
warn: (msg: string, ...args: unknown[]) => logger.warn(msg, ...args),
error: (msg: string, ...args: unknown[]) => logger.error(msg, ...args),
debug: (msg: string, ...args: unknown[]) => logger.debug(msg, ...args),
}
}