Simple Koa JSON Logging Middleware with development mode
npm install koa-json-logThis is a simple Koa middleware that outputs HTTP requests
and response details as JSON data. During development, logs are shown in a
human-readable format.
* Zero-dependency. Logs are output to stdout.
* Tools like PM2 can
then be used to write logs to files if needed.
* Development Mode (when NODE_ENV == development), shows human-readable logs.
* TypeScript definitions
NODE_ENV == development```
200 GET / - 4ms
404 GET /favicon.ico - 1ms
500 GET /test - 24ms
Error: Ack nooo!!
at router.get (/project/src/server/server.ts:18:11)
at dispatch (/project/node_modules/koa-router/node_modules/koa-compose/index.js:44:32)
at next (/project/node_modules/koa-router/node_modules/koa-compose/index.js:45:18)
at /project/node_modules/koa-router/lib/router.js:346:16
...
200 GET /static/logo.png - 4ms
!= development)(JSON data shown across multiple lines for readability)
`json`
{
"timestamp": "2018-05-04T12:22:14.412Z",
"method": "GET",
"url": "/",
"query": {},
"remoteAddress": "127.0.0.1",
"host": "localhost:3000",
"userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"statusCode": 200,
"responseTime": 7
}
{
"timestamp": "2018-05-04T12:25:23.125Z",
"method": "GET",
"url": "/test",
"query": {},
"remoteAddress": "127.0.0.1",
"host": "localhost:3000",
"userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
"errorMessage": "Ack nooo!!",
"errorStack": "Error: Ack nooo!!\n at router.get (/src/server/server.ts:18:11)\n ...",
"statusCode": 500,
"responseTime": 18
}
``
npm install koa-json-log
We recommend registering jsonLog() as one of the first app.use() calls, to
make sure all requests and errors are logged as expected.
`ts
import * as Koa from 'koa';
import { jsonLog } from 'koa-json-log';
const app = new Koa();
app.use(jsonLog());
...
`
The following options can be passed to jsonLog() (as an object):
json - boolean* - enable / disable JSON format (by default this is based on
NODE_ENV)onLog
- function* - you can override this function to intercept and modifylogFn
the log data object before it is written to the log.
- function* - override this function to redirect your log output. It isprocess.stdout.write
called with every log line. By default this is set to .
When throwing errors from middleware, jsonLog() will pick up and log theError
following properties from objects:
* message - the error messageexpose
- a boolean* indicating whether the error message should be returnedstatus
to the client in the HTTP response
* - the http status to returnstack
* - the error stack trace (never returned to the client)data` - additional JSON data to log (never returned to the client)
*
* More options
* Probably some other things!
Pull requests welcome!
MIT