winston是强大灵活的node日志库之一,支持多种传输通道,自定义日志格式,日志切割,多实例等。winston支持六种级别{error: 0, warn: 1, info: 2, http: 3, verbose: 4, debug: 5, silly: 6},内置error/warn/info可以直接使用levels属性方法写入日志
logger.info("this is a info")
logger.warn("this is a warn")
logger.error("this is a error")
winston 默认有 4 种传输通道:
Console:打印到控制台
File:记录到文件中
Http:通过 http 传输
Stream:通过流传输
malagu集成有winston组件,使得winston在malagu上使用更加简便
yarn add winston,winston-daily-rotate-file,malagu-winston
或者
npm install winston --save
npm install winston-daily-rotate-file --save
npm install malagu-winston --save
malagu:
logger:
winstonConfig:
level: 'info' //日志级别
dailyRotateConfig:
frequency: '24h'
filename: '%DATE%.log' //日志文件名
dirname: './logs' //日志文件目录
import { Component, LOGGER_CONFIG, Value } from "@malagu/core";
import { WinstonConfig } from "malagu-winston";
import { format, transports } from 'winston';
import * as Transport from 'winston-transport';
const DailyRotateFile = require('winston-daily-rotate-file');
@Component(WinstonConfig)
export class WinstonConfigImpl implements WinstonConfig{
transports: Transport[];
constructor(
// 引入malagu-*.yml中logger配置信息
@Value(LOGGER_CONFIG)
protected readonly config: any,
@Value('mode')
protected readonly mode: string
) {
const { dailyRotateConfig } = this.config;
this.transports = [
new DailyRotateFile({
...dailyRotateConfig,
// 定义日志格式
format: format.combine(
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss,SSS'}),
format.simple(),
format.printf(msg =>
`${msg.timestamp} - ${msg.level}: ${msg.message}`
)
),
})
];
if (this.mode.includes('test')) {
// 测试环境增加输出控制台通道
this.transports.push(new transports.Console({
format: format.combine(
format.colorize(),
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss,SSS'}),
format.simple(),
format.printf(msg =>
`${msg.timestamp} - ${msg.level}: ${msg.message}`
)
),
}));
};
}
}
import { Autowired, Logger } from "@malagu/core";
import { Controller, Get, Json, Param } from "@malagu/mvc/lib/node";
import { UserInfoService } from "../service";
@Controller("user")
export class UserController {
@Autowired(Logger)
protected logger: Logger;
@Autowired(UserInfoService)
protected userInfoService: UserInfoService;
@Get("/:userId")
@Json()
@Authenticated()
async getUserInfo(@Param("userId") userId: number){
this.logger.info(`获取用户信息:${userId}`)
const result = await this.userInfoService.getUserInfo(userId);
return result
}
}
日志输出位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PftXinb3-1660188506878)(/img/bVc1EMI)]
日志内容
2022-08-10 13:42:42,802 - info: GET /api/user/100000 ( 53092 on LAPTOP-EU7RHVSK) starting GET /api/user/100000 with traceId[08bfe038-47b4-4357-b8fe-a51b7618b8c2]
2022-08-10 13:42:42,864 - info: GET /api/user/100000 ( 53092 on LAPTOP-EU7RHVSK) with 08bfe038-47b4-4357-b8fe-a51b7618b8c2 获取用户信息:100000
2022-08-10 13:42:42,966 - info: GET /api/user/100000 ( 53092 on LAPTOP-EU7RHVSK) with 08bfe038-47b4-4357-b8fe-a51b7618b8c2 ending GET /api/user/100000 with traceId[08bfe038-47b4-4357-b8fe-a51b7618b8c2], cost 162ms
winston在malagu中的应用还是比较容易上手的,更多winston的使用请参考的乔珂力的《Node.js日志神器(winston)》
本文为学习类文章,如有错误欢迎指正!思考内容欢迎各位大佬答疑解惑。