我在自己的nodejs应用程序中使用Winston。
const winston = require('winston');
const logger = winston.createLogger({
transports: [
new winston.transports.Console()
]
});
logger.info('What rolls down stairs');
我想添加到日志相关ID,但是我不想每次都写
logger.info('What rolls down stairs', correlationId);
我想让温斯顿做到这一点。对于每个日志,我都希望获得该函数的结果相关性ID,以便可以将相关性ID发送给用户(而不仅仅是将其输出到控制台)。
const correlationId = logger.info('blabla')
温斯顿有可能做到这一点吗?
答案 0 :(得分:0)
要创建correlationId
并在Winston中默认设置每个日志,您需要使用defaultMeta
。
var correlationId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const winston = require('winston');
const logger = winston.createLogger({
defaultMeta: { correlationId },
transports: [
new winston.transports.Console()
]
});
logger.info('What rolls down stairs');
答案 1 :(得分:0)
import { v4 as uuidv4 } from "uuid";
export async function withCorrelationId(message:string) {
const id = uuidv4()
logger.info({
message,
id,
});
return id
}