在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:yidinghan/koa2-winston开源软件地址:https://github.com/yidinghan/koa2-winston开源编程语言:JavaScript 100.0%开源软件介绍:koa2-winstonkoa2 version winston logger like express-winston Add logger to your koa2 server in 3 lines UsageInstallationnpm i --save koa2-winston Quick Startconst { logger } = require('koa2-winston');
app.use(logger()); request log will look like {
"req": {
"header": {
"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": {
"header": {
"content-type": "application/json; charset=utf-8",
"content-length": "16"
},
"status": 200
},
"duration": 8,
"level": "info",
"message": "HTTP GET /hello"
} ConfigurationEach parameter has a default value, and you can customize your logger by changing the configuration app.use(
logger({
transports: new winston.transports.Console({ json: true, stringify: true }),
level: 'info',
reqKeys: [
'header',
'url',
'method',
'httpVersion',
'href',
'query',
'length',
],
reqSelect: [],
reqUnselect: ['header.cookie'],
resKeys: ['header', 'status'],
resSelect: [],
resUnselect: [],
})
); Many configuration explain can be found in logger ExamplesDo not record any request fieldsapp.use(
logger({
reqKeys: [],
})
); The req object will be empty {
"req": {},
"started_at": 1494486039864,
"res": {
"header": {
"content-type": "text/plain; charset=utf-8",
"content-length": "8"
},
"status": 200
},
"duration": 26,
"level": "info",
"message": "HTTP GET /"
} Do not record any response fieldsapp.use(
logger({
resKeys: [],
})
); The res object will be empty {
"req": {
"header": {
"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 /"
} Do not record UAapp.use(
logger({
reqUnselect: ['header.cookie', 'header.user-agent'],
})
); The UA of request will be ignored {
"req": {
"header": {
"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": {
"header": {
"content-type": "text/plain; charset=utf-8",
"content-length": "8"
},
"status": 200
},
"duration": 26,
"level": "info",
"message": "HTTP GET /"
} Record a response body filedapp.use(
logger({
resSelect: ['body.success'],
})
); The {
"req": {
"header": {
"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": {
"header": {
"content-type": "text/plain; charset=utf-8",
"content-length": "8"
},
"status": 200,
"body": {
// Any possible value given by the server
"success": false
}
},
"duration": 26,
"level": "info",
"message": "HTTP GET /"
} Simple BenchmarkAt node 8.2 middleware x 90,281 ops/sec ±7.89% (13 runs sampled) At node 8.4 middleware x 112,011 ops/sec ±10.26% (18 runs sampled) Schema StringifyWith fast-json-stringify support, default transport logger is much faster total ops/sec { jsonstringify: 73544 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
total ops/sec { schemastringify: 90223 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
v1.7.1 vs v2.4.0total ops/sec { 'v1.7.1': 111416 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
total ops/sec { 'v2.4.0': 131234 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
Math.floor vs parseIntRelated commit in HERE JSPerf link in HERE Testing in Chrome 70.0.3505 / Mac OS X 10.13.5
v3Finally, winston v3 support. But winston will install as dependencies not peerDependencies. With better backward compatibility, users don't have to worry about the new version of koa2-winston will conflict with other winston usage in the project. v3.1The total ops/sec { 'v3.0.2': 180020 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
total ops/sec { 'v3.1.0': 541854 }
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
Biggest change
JSDocTable of Contentsloggerlogger middleware for koa2 use winston Parameters
Examplesconst { logger } = require('koa2-winston');
app.use(logger());
// request logger look like down here
// {
// "req": {
// "header": {
// "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": {
// "header": {
// "content-type": "text/plain; charset=utf-8",
// "content-length": "8"
// },
// "status": 200
// },
// "duration": 26,
// "level": "info",
// "message": "HTTP GET /"
// } Returns function logger middleware asJsonSchemaPathParameters
ensureTypeObjectParameters
schemaKeysHandlerFnType: Function Parameters
schemaKeysHandlerParameters
generateSchemalogger middleware for koa2 use winston Parameters
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论