1.安装fastify-logger和pino-pretty包
npm install fastify-logger;
npm install pino-pretty;
2.代码示例Example.ts:
const logOpt = {
console: process.env.NODE_ENV !== 'production',
file : 'D://Test//test.log', //此处设置log文件输出路径
//logrotator设置按什么归档日志
logrotator : {
byDay: true,
dayDelimiter: '_'
},
maxBufferLength: 4096,
flushInterval: 1000,
//自定义level设置输出所有级别日志
customLevels: 'all',
//prettyPrint自定义日志输出格式,colorize设置为false为不显示颜色,防止日志文件出现 [32m 乱码
prettyPrint: {
colorize: false,
timestampKey: 'time',
translateTime : 'yyyy-mm-dd HH:MM:ss.l',
messageFormat: '{msg}'
}
};
const {opt} = require('fastify-logger')(logOpt);
//禁用流,stream和输出日志文件不可以同时使用
opt.stream = null;
const fastify = require('fastify')({
logger: opt
});