https://github.com/tilap/koa-request-log
Koa request logger
https://github.com/tilap/koa-request-log
koa-middleware koa2 koajs
Last synced: 5 months ago
JSON representation
Koa request logger
- Host: GitHub
- URL: https://github.com/tilap/koa-request-log
- Owner: tilap
- License: mit
- Created: 2016-04-25T10:55:34.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-10-19T00:27:45.000Z (over 7 years ago)
- Last Synced: 2025-08-27T05:27:14.458Z (11 months ago)
- Topics: koa-middleware, koa2, koajs
- Language: JavaScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Koa request logger [![NPM version][npm-image]][npm-url]
============================
Simple middleware **for Koa 2** to log in and out request, with diff time, and custom logger.
## Install
```
npm install --save koa-request-logger
```
## Usage
```
const Koa = require('Koa');
const koaRequestLogger = require('koa-request-logger');
const app = new Koa();
app.use(koaRequestLogger());
app.listen(3000);
```
## Options
You can customize :
- ```logger```, the logger object, by default ```console```
- ```method```, the logger method, by default ```log```
- ```format```, the output format function for IN and OUT request which takes params:
- ```ctx```: the koa context of the request
- ```id```: an unique ID to match in/out with easy
- ```isIn```: true if IN request, else false,
- ```timeDiff```: diff time for OUT requests (null for IN requests)
### Example with winston as logger and custom log format
```
const Koa = require('Koa');
const koaRequestLogger = require('koa-request-logger');
const winston = require('winston');
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
const app = new Koa();
app.use(koaRequestLogger({
logger,
method: 'info',
format: (ctx, id, isIn = true, timeDiff = null) => (isIn ?
`Incoming request #${id}: method ${ctx.request.method}, url ${ctx.request.url}` :
`Sending request results #${id}: method ${ctx.request.method}, url ${ctx.request.url}, status: ${ctx.status}, duration: ${timeDiff}ms`);
}));
app.listen(3000);
```
## License
MIT
[npm-image]: https://img.shields.io/npm/v/koa-request-logger.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-request-logger