winston | Yarn - JavaScript 软件包管理器 | Yarn中文文档 - Yarn中文网
简单用法示例
/**
* @description:日志输出服务
*/
const { createLogger, format, transports } = require("winston");
const { combine, timestamp, errors, colorize, padLevels, label, printf } =
format;
/**
* 协同服务日志格式
*/
const format1 = printf(({ level, message, timestamp, label }) => {
return `[${label}]|[ ${timestamp}]|[${level}]|[${message}]`;
});
/**
* 日志记录器
*/
const logger = createLogger({
level: "info",
format: combine(
timestamp({
format: "YYYY-MM-DD HH:mm:ss.SSS",
}),
label({ label: "icedog" }),
errors({ stack: true }),
format1
),
defaultMeta: { service: "icedog" },
transports: [
new transports.Console({
handleExceptions: true,
format: combine(colorize({ all: true }), padLevels()),
}),
],
// 是否程序报错退出
exitOnError: false,
// 是否不输出日志
silent: false,
});
module.exports = logger;
logger.info('app start');
发表评论