const { createLogger, format, transports } = require("winston")
const { combine, timestamp, prettyPrint } = format
const logger = createLogger({
format: combine(timestamp(), prettyPrint({ colorize: true }))
})
logger.add(new transports.Console())
logger.info("test message", { data: { a: 1, b: [1, 2, 3, 4], d: new Date() } })
印刷:
{ data: { a: 1, b: [ 1, 2, 3, 4 ], d: 2018-07-17T09:38:43.253Z },
level: 'info',
message: 'test message',
timestamp: '2018-07-17T09:38:43.253Z',
[Symbol(level)]: 'info',
[Symbol(splat)]:
[ { data: { a: 1, b: [ 1, 2, 3, 4 ], d: 2018-07-17T09:38:43.253Z } } ] }
我想从输出中删除一些字段:[SYMBOT(LEVEL)],[SYMBOT(SPLAT)],所以我想拥有这样的输出:
{ data: { a: 1, b: [ 1, 2, 3, 4 ], d: 2018-07-17T09:38:43.253Z },
level: 'info',
message: 'test message',
timestamp: '2018-07-17T09:38:43.253Z' }
您可以修改日志输出及其格式。这是官方文档:
使用此功能:
removeSymbols(meta) {
return Object.entries(meta).reduce(
(acc: LogMessage, [k, v]) =>
typeof k === 'symbol' ? acc : ((acc[k] = v), acc),
{}
);
}
winston使用
triple-beam
获取其符号。由于温斯顿的
import { SPLAT } from 'triple-beam';
import { inspect } from 'util';
winston.format.printf((info) => {
const {
timestamp,
ms,
context,
level,
message,
[SPLAT]: splat,
} = info;
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return `[${timestamp}] ${ms}\t${context}\t\t${level}\t${message} ${inspect(splat)}`;
})