Skip to content

Commit

Permalink
add a transporter to logger opts param (#65)
Browse files Browse the repository at this point in the history
* add transporter

* add unit test suit

* add transporter readme file

* lint

* remove no used
  • Loading branch information
moonrailgun authored and haoxin committed Mar 13, 2018
1 parent 3481627 commit 8862cc4
Show file tree
Hide file tree
Showing 4 changed files with 420 additions and 41 deletions.
24 changes: 24 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ app.use(logger())
Recommended that you `.use()` this middleware near the top
to "wrap" all subsequent middleware.

## Use Custom Transporter

```js
const logger = require('koa-logger')
const Koa = require('koa')

const app = new Koa()
app.use(logger((str, args) => {
// redirect koa logger to other output pipe
// default is process.stdout(by console.log function)
}))
```
or
```js
app.use(logger({
transporter: (str, args) => {
// ...
}
}))
```

Param `str` is output string with ANSI Color, and you can get pure text with other modules like `strip-ansi`
Param `args` is a array by `[format, method, url, status, time, length]`

## License

MIT
Expand Down
30 changes: 25 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Counter = require('passthrough-counter')
const humanize = require('humanize-number')
const bytes = require('bytes')
const chalk = require('chalk')
const util = require('util')

/**
* Expose logger.
Expand All @@ -33,10 +34,29 @@ const colorCodes = {
*/

function dev (opts) {
// print to console helper.
var print = (function () {
var transporter
if (typeof opts === 'function') {
transporter = opts
} else if (!!opts && !!opts.transporter) {
transporter = opts.transporter
}

return function printFunc (...args) {
var str = util.format(...args)
if (transporter) {
transporter(str, args)
} else {
console.log(...args)
}
}
}())

return async function logger (ctx, next) {
// request
const start = Date.now()
console.log(' ' + chalk.gray('<--') +
print(' ' + chalk.gray('<--') +
' ' + chalk.bold('%s') +
' ' + chalk.gray('%s'),
ctx.method,
Expand All @@ -46,7 +66,7 @@ function dev (opts) {
await next()
} catch (err) {
// log uncaught downstream errors
log(ctx, start, null, err)
log(print, ctx, start, null, err)
throw err
}

Expand Down Expand Up @@ -75,7 +95,7 @@ function dev (opts) {
function done (event) {
res.removeListener('finish', onfinish)
res.removeListener('close', onclose)
log(ctx, start, counter ? counter.length : length, null, event)
log(print, ctx, start, counter ? counter.length : length, null, event)
}
}
}
Expand All @@ -84,7 +104,7 @@ function dev (opts) {
* Log helper.
*/

function log (ctx, start, len, err, event) {
function log (print, ctx, start, len, err, event) {
// get the status code of the response
const status = err
? (err.isBoom ? err.output.statusCode : err.status || 500)
Expand All @@ -108,7 +128,7 @@ function log (ctx, start, len, err, event) {
: event === 'close' ? chalk.yellow('-x-')
: chalk.gray('-->')

console.log(' ' + upstream +
print(' ' + upstream +
' ' + chalk.bold('%s') +
' ' + chalk.gray('%s') +
' ' + chalk[color]('%s') +
Expand Down
70 changes: 36 additions & 34 deletions test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,39 @@ const Boom = require('boom')
const _ = require('koa-route')
const logger = require('./index')

const app = new Koa()
app.use(logger())

app.use(_.get('/200', function (ctx) {
ctx.body = 'hello world'
}))

app.use(_.get('/301', function (ctx) {
ctx.status = 301
}))

app.use(_.get('/304', function (ctx) {
ctx.status = 304
}))

app.use(_.get('/404', function (ctx) {
ctx.status = 404
ctx.body = 'not found'
}))

app.use(_.get('/500', function (ctx) {
ctx.status = 500
ctx.body = 'server error'
}))

app.use(_.get('/500-boom', function (ctx) {
ctx.throw(Boom.badImplementation('terrible implementation'))
}))

app.use(_.get('/error', function (ctx) {
throw new Error('oh no')
}))

module.exports = app
module.exports = function (opts) {
const app = new Koa()
app.use(logger(opts))

app.use(_.get('/200', function (ctx) {
ctx.body = 'hello world'
}))

app.use(_.get('/301', function (ctx) {
ctx.status = 301
}))

app.use(_.get('/304', function (ctx) {
ctx.status = 304
}))

app.use(_.get('/404', function (ctx) {
ctx.status = 404
ctx.body = 'not found'
}))

app.use(_.get('/500', function (ctx) {
ctx.status = 500
ctx.body = 'server error'
}))

app.use(_.get('/500-boom', function (ctx) {
ctx.throw(Boom.badImplementation('terrible implementation'))
}))

app.use(_.get('/error', function (ctx) {
throw new Error('oh no')
}))

return app
}
Loading

0 comments on commit 8862cc4

Please sign in to comment.