Skip to content

Commit

Permalink
Merge branch 'prior-instance' of https://github.com/jsumners/hapi-pino
Browse files Browse the repository at this point in the history
…into jsumners-prior-instance
  • Loading branch information
mcollina committed May 26, 2016
2 parents afaf4a8 + bf0c552 commit cf2708c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ events"](#hapievents) section.
mappings. The default level tags are exposed via `hapi-pino.levelTags`.
- `[allTags]` - the logging level to apply to all tags not matched by
`tags`, defaults to `'info'`.
- `[instance]` - uses a previously created Pino instance as the logger.
The instance's `stream` and `serializers` take precedence.


<a name="serverdecorations"></a>
Expand Down
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ module.exports.levelTags = {
}

function register (server, options, next) {
options.stream = options.stream || process.stdout
options.serializers = options.serializers || {}
options.serializers.req = asReqValue
options.serializers.res = pino.stdSerializers.res
options.serializers.err = pino.stdSerializers.err

let logger
if (options.instance) {
options.instance.serializers = Object.assign(options.serializers, options.instance.serializers)
logger = options.instance
} else {
options.stream = options.stream || process.stdout
let stream = options.stream || process.stdout

if (options.prettyPrint) {
let pretty = pino.pretty()
pretty.pipe(stream)
stream = pretty
}

logger = pino(options, stream)
}

const tagToLevels = Object.assign({}, module.exports.levelTags, options.tags)
const allTags = options.allTags || 'info'

Expand All @@ -26,16 +42,6 @@ function register (server, options, next) {
return next(new Error('invalid tag levels'))
}

let stream = options.stream || process.stdout

if (options.prettyPrint) {
let pretty = pino.pretty()
pretty.pipe(stream)
stream = pretty
}

const logger = pino(options, stream)

// expose logger as 'server.app.logger'
server.app.logger = logger

Expand Down
53 changes: 53 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,56 @@ experiment('logs through request.log', () => {
})
})
})

experiment('uses a prior pino instance', () => {
test('without pre-defined serializers', (done) => {
const server = getServer()
const stream = sink((data) => {
expect(data.data).to.equal('hello world')
expect(data.level).to.equal(30)
done()
})
const logger = require('pino')(stream)
const plugin = {
register: Pino.register,
options: {
instance: logger
}
}

server.register(plugin, (err) => {
expect(err).to.be.undefined()
server.log(['something'], 'hello world')
})
})

test('with pre-defined serializers', (done) => {
const server = getServer()
const stream = sink((data) => {
expect(data.msg).to.equal('hello world')
expect(data.foo).to.exist()
expect(data.foo.serializedFoo).to.exist()
expect(data.foo.serializedFoo).to.equal('foo is bar')
expect(data.level).to.equal(30)
done()
})
const logger = require('pino')({
serializers: {
foo: (input) => {
return { serializedFoo: `foo is ${input}` }
}
}
}, stream)
const plugin = {
register: Pino.register,
options: {
instance: logger
}
}

server.register(plugin, (err) => {
expect(err).to.be.undefined()
server.app.logger.info({foo: 'bar'}, 'hello world')
})
})
})

0 comments on commit cf2708c

Please sign in to comment.