{"id":15022287,"url":"https://github.com/yidinghan/koa2-winston","last_synced_at":"2025-04-12T12:32:02.149Z","repository":{"id":19741650,"uuid":"87773057","full_name":"yidinghan/koa2-winston","owner":"yidinghan","description":"koa2 version winston logger like express-winston","archived":false,"fork":false,"pushed_at":"2023-10-18T06:33:21.000Z","size":1568,"stargazers_count":38,"open_issues_count":19,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T20:04:05.766Z","etag":null,"topics":["fast-json-stringify","koa","koa2","logger","nodejs","winston"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidinghan.png","metadata":{"files":{"readme":"README.CN.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-10T06:10:29.000Z","updated_at":"2024-06-18T20:06:38.849Z","dependencies_parsed_at":"2024-06-18T20:16:19.434Z","dependency_job_id":null,"html_url":"https://github.com/yidinghan/koa2-winston","commit_stats":{"total_commits":248,"total_committers":7,"mean_commits":35.42857142857143,"dds":"0.056451612903225756","last_synced_commit":"84899e5007f07f2eabb4d3c756000d6a60b43898"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fkoa2-winston","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fkoa2-winston/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fkoa2-winston/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fkoa2-winston/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidinghan","download_url":"https://codeload.github.com/yidinghan/koa2-winston/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248566575,"owners_count":21125690,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fast-json-stringify","koa","koa2","logger","nodejs","winston"],"created_at":"2024-09-24T19:57:44.795Z","updated_at":"2025-04-12T12:32:01.197Z","avatar_url":"https://github.com/yidinghan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa2-winston\n\n[![Travis](https://img.shields.io/travis/yidinghan/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![npm](https://img.shields.io/npm/l/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![npm](https://img.shields.io/npm/v/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![npm](https://img.shields.io/npm/dm/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![David](https://img.shields.io/david/yidinghan/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![David](https://img.shields.io/david/dev/yidinghan/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n[![node](https://img.shields.io/node/v/koa2-winston.svg?style=flat-square)](https://www.npmjs.com/package/koa2-winston)\n\nkoa2 版本的 winston logger, 和 [express-winston](https://github.com/bithavoc/express-winston) 类似\n\n在3行内将logger添加到koa2服务器\n\n\u003c!-- TOC --\u003e\n\n- [koa2-winston](#koa2-winston)\n- [用法](#用法)\n  - [安装](#安装)\n  - [快速开始](#快速开始)\n  - [配置](#配置)\n  - [例子](#例子)\n    - [不记录任何请求内容](#不记录任何请求内容)\n    - [不记录任何响应内容](#不记录任何响应内容)\n    - [不记录 UA](#不记录-ua)\n    - [额外记录一个响应的字段](#额外记录一个响应的字段)\n- [JSDoc](#jsdoc)\n  - [keysRecorder](#keysrecorder)\n  - [logger](#logger)\n\n\u003c!-- /TOC --\u003e\n\n# 用法\n\n## 安装\n\n```shell\nnpm i --save koa2-winston\n```\n\n## 快速开始\n\n```js\nconst { logger } = require('koa2-winston');\napp.use(logger());\n```\n\n访问的日志将会如下出现\n```json\n{\n  \"req\": {\n    \"headers\": {\n      \"host\": \"localhost:3000\",\n      \"connection\": \"keep-alive\",\n      \"upgrade-insecure-requests\": \"1\",\n      \"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\",\n      \"accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\",\n      \"dnt\": \"1\",\n      \"accept-encoding\": \"gzip, deflate, sdch, br\",\n      \"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\"\n    },\n    \"url\": \"/hello\",\n    \"method\": \"GET\",\n    \"href\": \"http://localhost:3000/hello\",\n    \"query\": {}\n  },\n  \"started_at\": 1494554053492,\n  \"res\": {\n    \"headers\": {\n      \"content-type\": \"application/json; charset=utf-8\",\n      \"content-length\": \"16\"\n    },\n    \"status\": 200\n  },\n  \"duration\": 8,\n  \"level\": \"info\",\n  \"message\": \"HTTP GET /hello\"\n}\n```\n\n## 配置\n\n每一个变量都有一个默认值，你可以通过配置不同的变量自定义你的日志记录器\n\n```js\napp.use(logger({\n  transports: new winston.transports.Console({ json: true, stringify: true }),\n  level: 'info',\n  reqKeys: ['headers','url','method', 'httpVersion','href','query','length'],\n  reqSelect: [],\n  reqUnselect: ['headers.cookie'],\n  resKeys: ['headers','status'],\n  resSelect: [],\n  resUnselect: [],\n}));\n```\n\n更多配置解析，可以在[logger](#logger)中查看\n\n## 例子\n\n### 不记录任何请求内容\n\n```js\napp.use(logger({\n  reqKeys: []\n}));\n```\n\n`req` 对象将会为空\n\n```json\n{\n  \"req\": {\n  },\n  \"started_at\": 1494486039864,\n  \"res\": {\n    \"headers\": {\n      \"content-type\": \"text/plain; charset=utf-8\",\n      \"content-length\": \"8\"\n    },\n    \"status\": 200\n  },\n  \"duration\": 26,\n  \"level\": \"info\",\n  \"message\": \"HTTP GET /\"\n}\n```\n\n### 不记录任何响应内容\n```js\napp.use(logger({\n  resKeys: []\n}));\n```\n\n`res` 对象将会为空\n\n```json\n{\n  \"req\": {\n    \"headers\": {\n      \"host\": \"127.0.0.1:59534\",\n      \"accept-encoding\": \"gzip, deflate\",\n      \"user-agent\": \"node-superagent/3.5.2\",\n      \"connection\": \"close\"\n    },\n    \"url\": \"/\",\n    \"method\": \"GET\",\n    \"href\": \"http://127.0.0.1:59534/\",\n    \"query\": {}\n  },\n  \"started_at\": 1494486039864,\n  \"res\": {\n  },\n  \"duration\": 26,\n  \"level\": \"info\",\n  \"message\": \"HTTP GET /\"\n}\n```\n\n### 不记录 UA\n\n```js\napp.use(logger({\n  reqUnselect: ['headers.cookies', 'headers.user-agent']\n}));\n```\n\n请求的 UA 将会被忽略\n\n```json\n{\n  \"req\": {\n    \"headers\": {\n      \"host\": \"127.0.0.1:59534\",\n      \"accept-encoding\": \"gzip, deflate\",\n      \"connection\": \"close\"\n    },\n    \"url\": \"/\",\n    \"method\": \"GET\",\n    \"href\": \"http://127.0.0.1:59534/\",\n    \"query\": {}\n  },\n  \"started_at\": 1494486039864,\n  \"res\": {\n    \"headers\": {\n      \"content-type\": \"text/plain; charset=utf-8\",\n      \"content-length\": \"8\"\n    },\n    \"status\": 200\n  },\n  \"duration\": 26,\n  \"level\": \"info\",\n  \"message\": \"HTTP GET /\"\n}\n```\n\n### 额外记录一个响应的字段\n\n```js\napp.use(logger({\n  resSelect: ['body.success']\n}));\n```\n\n`body` 里面的 `success` 字段将会被记录\n\n```json\n{\n  \"req\": {\n    \"headers\": {\n      \"host\": \"127.0.0.1:59534\",\n      \"accept-encoding\": \"gzip, deflate\",\n      \"connection\": \"close\"\n    },\n    \"url\": \"/\",\n    \"method\": \"GET\",\n    \"href\": \"http://127.0.0.1:59534/\",\n    \"query\": {}\n  },\n  \"started_at\": 1494486039864,\n  \"res\": {\n    \"headers\": {\n      \"content-type\": \"text/plain; charset=utf-8\",\n      \"content-length\": \"8\"\n    },\n    \"status\": 200,\n    \"body\": {\n      // 会记录下任何服务器响应的值\n      \"success\": false\n    }\n  },\n  \"duration\": 26,\n  \"level\": \"info\",\n  \"message\": \"HTTP GET /\"\n}\n```\n\n# JSDoc\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n## keysRecorder\n\nkeysRecorder\nuse ldoash pick, get and set to collect data from given target object\n\n**Parameters**\n\n-   `payload` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** input arguments (optional, default `{}`)\n    -   `payload.defaults` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u0026lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)\u003e?** default keys will be collected\n    -   `payload.selects` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u0026lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)\u003e?** keys will be collected as\n        additional part\n    -   `payload.unselects` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u0026lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)\u003e?** keys that will be ignored at last\n\n**Examples**\n\n```javascript\n// without payload\nconst recorder = keysRecorder();\nrecorder() // {}\nrecorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // {}\n\n// with defaults\nconst recorder = keysRecorder({ defaults: ['foo'] });\nrecorder() // {}\nrecorder({ foo: 1, bar: 2, foobar: { a: 3, b: 4 } }) // { foo: 1 }\n\n// with defaults and selects\nconst recorder = keysRecorder({ defaults: ['foo'], selects: ['foobar'] });\nrecorder() // {}\nrecorder({\n  foo: 1,\n  bar: 2,\n  foobar: { a: 3, b: 4 }\n}) // { foo: 1, foobar: { a: 3, b: 4 } }\n\n// with defaults and unselects\nconst recorder = keysRecorder({ defaults: ['foobar'], unselects: ['foobar.a'] });\nrecorder() // {}\nrecorder({\n  foo: 1,\n  bar: 2,\n  foobar: { a: 3, b: 4 }\n}) // { foobar: { a: 3 } }\n\n// with defaults and selects and unselects\nconst recorder = keysRecorder({\n  defaults: ['foo'],\n  selects: ['foobar'],\n  unselects: ['foobar.b'],\n});\nrecorder() // {}\nrecorder({\n  foo: 1,\n  bar: 2,\n  foobar: { a: 3, b: 4 }\n}) // { foo: 1, foobar: { a: 3 } }\n```\n\nReturns **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** closure function, setting by given payload\n\n## logger\n\nlogger middleware for koa2 use winston\n\n**Parameters**\n\n-   `payload` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** input arguments (optional, default `{}`)\n    -   `payload.transports` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u0026lt;[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** winston transports instance (optional, default `winston.transports.Console`)\n    -   `payload.level` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** default log level of logger (optional, default `'info'`)\n    -   `payload.reqKeys` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** default request fields to be logged (optional, default `['headers','url','method',\n        'httpVersion','href','query','length']`)\n    -   `payload.reqSelect` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** additional request fields to be logged (optional, default `[]`)\n    -   `payload.reqUnselect` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** request field\n                         will be removed from the log (optional, default `['headers.cookie']`)\n    -   `payload.resKeys` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** default response fields to be logged (optional, default `['headers','status']`)\n    -   `payload.resSelect` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** additional response fields to be logged (optional, default `[]`)\n    -   `payload.resUnselect` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** response field will be removed from the log (optional, default `[]`)\n\n**Examples**\n\n```javascript\nconst { logger } = require('koa2-winston');\napp.use(logger());\n// trrific logger look like down here\n// {\n//   \"req\": {\n//     \"headers\": {\n//       \"host\": \"127.0.0.1:59534\",\n//       \"accept-encoding\": \"gzip, deflate\",\n//       \"user-agent\": \"node-superagent/3.5.2\",\n//       \"connection\": \"close\"\n//     },\n//     \"url\": \"/\",\n//     \"method\": \"GET\",\n//     \"href\": \"http://127.0.0.1:59534/\",\n//     \"query\": {}\n//   },\n//   \"started_at\": 1494486039864,\n//   \"res\": {\n//     \"headers\": {\n//       \"content-type\": \"text/plain; charset=utf-8\",\n//       \"content-length\": \"8\"\n//     },\n//     \"status\": 200\n//   },\n//   \"duration\": 26,\n//   \"level\": \"info\",\n//   \"message\": \"HTTP GET /\"\n// }\n```\n\nReturns **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** logger middleware\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fkoa2-winston","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidinghan%2Fkoa2-winston","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fkoa2-winston/lists"}