1.0.2 • Published 5 years ago
@doddle/koa-winston v1.0.2
@doddle/koa-winston
两个改动点
- 在koa2-winston 基础上,新增了自定义日志信息的功能, see here;
- 将日志日期输出属性start_at打印格式从时间戳换成了 formatYYYY-MM-DD HH:mm:ss
koa2 版本的 winston logger, 和 express-winston 类似
在3行内将logger添加到koa2服务器
用法
安装
npm i --save @doddle/koa-winston快速开始
const { logger } = require('@doddle/koa-winston');
app.use(logger());访问的日志将会如下出现
{
  "req": {
    "headers": {
      "host": "localhost:3000",
      "connection": "keep-alive",
      "upgrade-insecure-requests": "1",
      "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
      "dnt": "1",
      "accept-encoding": "gzip, deflate, sdch, br",
      "accept-language": "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,de;q=0.2,ja;q=0.2,it;q=0.2"
    },
    "url": "/hello",
    "method": "GET",
    "href": "http://localhost:3000/hello",
    "query": {}
  },
  "started_at": 1494554053492,
  "res": {
    "headers": {
      "content-type": "application/json; charset=utf-8",
      "content-length": "16"
    },
    "status": 200
  },
  "duration": 8,
  "level": "info",
  "message": "HTTP GET /hello"
}配置
每一个变量都有一个默认值,你可以通过配置不同的变量自定义你的日志记录器
app.use(logger({
  transports: new winston.transports.Console({ json: true, stringify: true }),
  level: 'info',
  reqKeys: ['headers','url','method', 'httpVersion','href','query','length'],
  reqSelect: [],
  reqUnselect: ['headers.cookie'],
  resKeys: ['headers','status'],
  resSelect: [],
  resUnselect: [],
}));更多配置解析,可以在logger中查看
例子
不记录任何请求内容
app.use(logger({
  reqKeys: []
}));req 对象将会为空
{
  "req": {
  },
  "started_at": 1494486039864,
  "res": {
    "headers": {
      "content-type": "text/plain; charset=utf-8",
      "content-length": "8"
    },
    "status": 200
  },
  "duration": 26,
  "level": "info",
  "message": "HTTP GET /"
}不记录任何响应内容
app.use(logger({
  resKeys: []
}));res 对象将会为空
{
  "req": {
    "headers": {
      "host": "127.0.0.1:59534",
      "accept-encoding": "gzip, deflate",
      "user-agent": "node-superagent/3.5.2",
      "connection": "close"
    },
    "url": "/",
    "method": "GET",
    "href": "http://127.0.0.1:59534/",
    "query": {}
  },
  "started_at": 1494486039864,
  "res": {
  },
  "duration": 26,
  "level": "info",
  "message": "HTTP GET /"
}不记录 UA
app.use(logger({
  reqUnselect: ['headers.cookies', 'headers.user-agent']
}));请求的 UA 将会被忽略
{
  "req": {
    "headers": {
      "host": "127.0.0.1:59534",
      "accept-encoding": "gzip, deflate",
      "connection": "close"
    },
    "url": "/",
    "method": "GET",
    "href": "http://127.0.0.1:59534/",
    "query": {}
  },
  "started_at": 1494486039864,
  "res": {
    "headers": {
      "content-type": "text/plain; charset=utf-8",
      "content-length": "8"
    },
    "status": 200
  },
  "duration": 26,
  "level": "info",
  "message": "HTTP GET /"
}额外记录一个响应的字段
app.use(logger({
  resSelect: ['body.success']
}));body 里面的 success 字段将会被记录
{
  "req": {
    "headers": {
      "host": "127.0.0.1:59534",
      "accept-encoding": "gzip, deflate",
      "connection": "close"
    },
    "url": "/",
    "method": "GET",
    "href": "http://127.0.0.1:59534/",
    "query": {}
  },
  "started_at": 1494486039864,
  "res": {
    "headers": {
      "content-type": "text/plain; charset=utf-8",
      "content-length": "8"
    },
    "status": 200,
    "body": {
      // 会记录下任何服务器响应的值
      "success": false
    }
  },
  "duration": 26,
  "level": "info",
  "message": "HTTP GET /"
}新增例子
添加自定义的日志信息
const { logger, addExtendProperty } = require('');
addExtendProperty({
  author_name: { type: 'string' }
})
app.use(
  logger({
    addExtendInfo: (ctx) => ({
      author_name: ctx.user && ctx.user.name,
  }),
  })
);The req object will be empty
{
  "req": {},
  "started_at": "YYYY-MM-DD HH:mm:ss",
  "author_name": "doddle",
  "res": {
    "header": {
      "content-type": "text/plain; charset=utf-8",
      "content-length": "8"
    },
    "status": 200
  },
  "duration": 26,
  "level": "info",
  "message": "HTTP GET /"
}JSDoc
keysRecorder
keysRecorder use ldoash pick, get and set to collect data from given target object
Parameters
- payloadObject input arguments (optional, default- {})
Examples
// without payload
const recorder = keysRecorder();
recorder() // {}
recorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // {}
// with defaults
const recorder = keysRecorder({ defaults: ['foo'] });
recorder() // {}
recorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // { foo: 1 }
// with defaults and selects
const recorder = keysRecorder({ defaults: ['foo'], selects: ['foobar'] });
recorder() // {}
recorder({
  foo: 1,
  bar: 2,
  foobar: { a: 3, b: 4 }
}) // { foo: 1, foobar: { a: 3, b: 4 } }
// with defaults and unselects
const recorder = keysRecorder({ defaults: ['foobar'], unselects: ['foobar.a'] });
recorder() // {}
recorder({
  foo: 1,
  bar: 2,
  foobar: { a: 3, b: 4 }
}) // { foobar: { a: 3 } }
// with defaults and selects and unselects
const recorder = keysRecorder({
  defaults: ['foo'],
  selects: ['foobar'],
  unselects: ['foobar.b'],
});
recorder() // {}
recorder({
  foo: 1,
  bar: 2,
  foobar: { a: 3, b: 4 }
}) // { foo: 1, foobar: { a: 3 } }Returns function closure function, setting by given payload
logger
logger middleware for koa2 use winston
Parameters
- payloadobject input arguments (optional, default- {})- payload.transportsArray<object> winston transports instance (optional, default- winston.transports.Console)
- payload.levelstring default log level of logger (optional, default- 'info')
- payload.reqKeysstring default request fields to be logged (optional, default- ['headers','url','method', 'httpVersion','href','query','length'])
- payload.reqSelectstring additional request fields to be logged (optional, default- [])
- payload.reqUnselectstring request field will be removed from the log (optional, default- ['headers.cookie'])
- payload.resKeysstring default response fields to be logged (optional, default- ['headers','status'])
- payload.resSelectstring additional response fields to be logged (optional, default- [])
- payload.resUnselectstring response field will be removed from the log (optional, default- [])
 
Examples
const { logger } = require('@doddle/koa-winston');
app.use(logger());
// trrific logger look like down here
// {
//   "req": {
//     "headers": {
//       "host": "127.0.0.1:59534",
//       "accept-encoding": "gzip, deflate",
//       "user-agent": "node-superagent/3.5.2",
//       "connection": "close"
//     },
//     "url": "/",
//     "method": "GET",
//     "href": "http://127.0.0.1:59534/",
//     "query": {}
//   },
//   "started_at": 1494486039864,
//   "res": {
//     "headers": {
//       "content-type": "text/plain; charset=utf-8",
//       "content-length": "8"
//     },
//     "status": 200
//   },
//   "duration": 26,
//   "level": "info",
//   "message": "HTTP GET /"
// }Returns function logger middleware