A logger middleware for express.Similar with koa-logger.
[2019-7-22 17:24:30] <-- GET /
[2019-7-22 17:24:31] --> GET / 200 835ms
[2019-7-22 17:24:48] <-- GET /
[2019-7-22 17:24:49] --> GET / 200 960ms
[2019-7-22 17:25:27] <-- GET /api
[2019-7-22 17:25:28] --> GET /api 200 357ms
[2019-7-22 17:25:53] <-- GET /users?page=2
[2019-7-22 17:25:54] --> GET /users?page=2 200 466ms
# Use npm
$ npm i express-simple-logger
# Use yarn
$ yarn add express-simple-logger
const express = require('express')
const logger = require('express-simple-logger')
const app = express()
app.use(logger())
Recommended that you .use()
this middleware near the top
to "wrap" all subsequent middleware.
params | type | required | default | description |
---|---|---|---|---|
unless | Array | false | [] | ignore path list |
logTime | Boolean | false | true | whether to log time |
logger | Function | false | -- | custom logger (str:String, args:Array) |
Param str
is output string, args.join(' ')
Param args
is a array by [logTime, format, method, url, status, time]
, when opt.logTime is false, logTime = ''
const http = require('http')
...
app.use(logger({
unless: ['/ignorepath'],
logTime: false,
// custom logger
// add short description of status code
logger (str, args) {
if (args.length === 6) {
// response logger
args[4] = args[4] + ' ' + http.STATUS_CODES[args[4]]
console.log(...args)
}
}
}))
MIT