我在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);我想让温斯顿这么做。对于每个日志,我希望获得作为函数结果的correlationId,这样我就可以将correlationId发送给用户(而不仅仅是将其输出到控制台)。
const correlationId = logger.info('blabla')使用Winston可以做到这一点吗?
发布于 2020-03-12 18:37:43
要在Winston need log中默认创建correlationId和setup,您需要使用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');发布于 2021-04-02 20:51:02
import { v4 as uuidv4 } from "uuid";
export async function withCorrelationId(message:string) {
const id = uuidv4()
logger.info({
message,
id,
});
return id
}https://stackoverflow.com/questions/60649786
复制相似问题