koa-api-logger
v2.0.0
Published
Api Logger & Monitor for Koa 2 (can work with router/joi router)
Downloads
9
Readme
koa-api-logger
Usage
Install
yarn add koa-api-logger
# or
npm i -S koa-api-logger
Use
const Koa = require('koa');
const router = require('koa-router')();
const logger = require('koa-api-logger');
const app = new Koa();
app.use(
logger({
// options
})
);
router.get('/user/:name', ctx => {
ctx.body = name;
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
Options
{
err: async (ctx, err) => {
// codes to handle error
},
// Such as: ['/upload']
ignore: [],
redis: {
host: '127.0.0.1',
port: 6379,
db: 0, // db to store
keyPrefix: 'kal:', // redis store prefix
ttl: 86400 * 180 // saving logs for 180 days
}
}
Design
Redis Hash Table
key:20170808
| key | val | | -------- | ------------------------------------- | | total | {count:1,success:1,avg:1,max:1,min:1} | | /#{path} | {count:1,success:1,avg:1,max:1,min:1} |
Monitor
const monitor = require('koa-api-logger/monitor');
const appName = monitor({
host: '127.0.0.1',
port: 6379,
db: 0,
keyPrefix: 'kal:'
});
appName('20170808').then(data => console.log);
/* like:
[
{
key: 'total',
data: { count: 2, success: 2, avg: 0.5, max: 1, min: 0 }
},
{
key: 'GET::/user/:name',
data: { count: 3, success: 3, avg: 0.3333333333333333, max: 1, min: 0 }
}
]
*/
History:
appName.history().then(data => console.log);
/* like:
[
'20170504',
'20170505'
]
*/
License
MIT
Donate Via Alipay: