fastify-logger
Install
bnpm install fastify-logger --save
Usage
// let logOpt = console: processenvNODE_ENV !== 'production' // 是否开启console.log 。。。 level: 'debug' streams: // fastify-logger/lib/LogStream.js 的子类 {} // 自定义format genReqId: { // 请求id生成器 var maxInt = 2147483647 var nextReqId = 0 return { return nextReqId = nextReqId + 1 & maxInt } } serializers: // 需要的额外数据 { return method: reqxxx } file: path // 文件路径 maxBufferLength: 4096 // 日志写入缓存队列最大长度 flushInterval: 1000 // flush间隔 logrotator: // 分割配置 byHour: true byDay: false hourDelimiter: '_' // 其实只需要配置一下 file字段, 因为线上环境,路径不同,不配置的话使用默认路径logOpt = file: '/path/to/log/lalala.log' const opt hook = logOpt;const fastify = logger: opt; // Declare a routefastify// Run the server!fastify
扩展stream
customSteam.js
const LogStream = { superoptions // ...自己的逻辑代码 } // 复写log方法 { // 想怎么写就怎么写 }