Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default tag mappings #7

Merged
merged 4 commits into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ events"](#hapievents) section.
- `[prettyPrint]` - pretty print the logs (same as `node server |
pino`), disable in production. Default is `false`, enable in
development by passing `true`.
- `[tags]` - a map to specify pairs of Hapi log tags and levels.
- `[tags]` - a map to specify pairs of Hapi log tags and levels. By default,
the tags *trace*, *debug*, *info*, *warn*, and *error* map to their
corresponding level. Any mappings you supply take precedence over the default
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'`.

Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
const pino = require('pino')

const levels = ['trace', 'debug', 'info', 'warn', 'error']
module.exports.levelTags = {
trace: 'trace',
debug: 'debug',
info: 'info',
warn: 'warn',
error: 'error'
}

function register (server, options, next) {
options.stream = options.stream || process.stdout
Expand All @@ -11,7 +18,7 @@ function register (server, options, next) {
options.serializers.res = pino.stdSerializers.res
options.serializers.err = pino.stdSerializers.err

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

const validTags = Object.keys(tagToLevels).filter((key) => levels.indexOf(tagToLevels[key]) < 0).length === 0
Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,25 @@ experiment('logs through request.log', () => {
server.inject('/')
})
})

test('uses default tag mapping', (done) => {
const server = getServer()
const stream = sink((data) => {
expect(data.data).to.equal('hello world')
expect(data.level).to.equal(20)
done()
})
const plugin = {
register: Pino.register,
options: {
stream: stream,
level: 'debug'
}
}

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