hapi-logger
v2.1.0
Published
A Hapi plugin for writing logs via bunyan
Downloads
39
Maintainers
Readme
hapi-logger
A Hapi plugin for writing logs to stdout via bunyan
Bunyan is a great module for logging, but I like the flexibility that having tagged log messages gives me over your traditional log levels. Luckily Hapi already emits log events with tags, so we can add those tags to our log message. The current implementation of this module logs every message as "info" as far as bunyan is concerned, so you might as well ignore the "level" property on the log message. However, all the tags are included on the message, which should give enough context about the message.
Contributing
This module makes use of a Makefile
for building/testing purposes. After obtaining a copy of the repo, run the following commands to make sure everything is in working condition before you start your work:
make install
make test
Before committing a change to your fork/branch, run the following commands to make sure nothing is broken:
make test
make test-cov
Don't forget to bump the version in the package.json
using the semver spec as a guide for which part to bump. Submit a pull request when your work is complete.
Notes:
- Please do your best to ensure the code coverage does not drop. If new unit tests are required to maintain the same level of coverage, please include those in your pull request.
- Please follow the same coding/formatting practices that have been established in the module.
Installation
npm install hapi-logger
Usage
To install this plugin on your Hapi server, do something similar to this:
var Hapi = require('hapi');
var server = new Hapi.Server();
var hapiLoggerConfig = {};
server.register({ register: require('hapi-logger'), options: hapiLoggerConfig }, function(err) {
if (err) {
console.log('error', 'Failed loading plugin: hapi-logger');
}
});
Plugin Options
name
The name of your application, or any other name you wish to have contained within your log message, for this pack of servers. Defaults to 'hapi-logger'
src
A flag that tells bunyan whether or not to include the location from where the log message originiated. Don't set this to true
in production! Defaults to false
tags
The tags used to determine when a log message is logged. This value can contain both strings and arrays of strings. The items in the top level of the collection are OR'd, while any items in nested arrays are AND'd.
If the collection includes '*'
then all messages will be logged. Defaults to ['*']
Examples
- Top-level strings only:
[
'error',
'info'
]
Means that log messages with either the tag 'error'
OR 'info'
will get logged.
- Nested strings only:
[
[
'error',
'request'
]
]
Means that log messages with the tags 'error'
AND 'request'
will get logged.
- Top-level and nested strings:
[
'info',
[
'error',
'request'
]
]
Means that log messages with either the tag 'info'
OR the tags 'error'
AND 'request'
will get logged.
ignoredTags
The tags used to determine when a log message is NOT logged. This value can contain both strings and arrays of strings. The items in the top level of the collection are OR'd, while any items in nested arrays are AND'd.
If the collection includes '*'
then no messages will be logged. Defaults to []
Note: If a log message matches any of these tags, it will not be logged, even if it would have been because it matched values in tags
.
Examples
- Top-level strings only:
[
'error',
'info'
]
Means that log messages with either the tag 'error'
OR 'info'
will not get logged.
- Nested strings only:
[
[
'error',
'request'
]
]
Means that log messages with the tags 'error'
AND 'request'
will not get logged.
- Top-level and nested strings:
[
'info',
[
'error',
'request'
]
]
Means that log messages with either the tag 'info'
OR the tags 'error'
AND 'request'
will not get logged.
shouldLog
A function that will receive the log data as a parameter and should return true
if the message should be logged, or false
if the message should not be logged. The tags
and ignoredTags
are evaulated first then this function will be executed. Defaults to null
Example
A Hapi route configured like this:
server.route({
method: 'GET',
path: '/test/{param}',
handler: function(request, reply) {
request.log(['get', 'testResource'], 'Some important info...');
reply('Success!');
}
});
would cause the following log message to be written (in addition to any other internal Hapi-related events) when a request is issued to the route:
{"name":"hapi-logger","hostname":"Me","pid":54705,"level":30,"tags":["get","testResource"],"req":{"id":"1408481983531-54705-47711","headers":{"user-agent":"curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5","host":"localhost:8080","accept":"*/*"},"method":"get","info":{"received":1408481983531,"remoteAddress":"127.0.0.1","remotePort":63014,"referrer":"","host":"localhost:8080"},"path":"/test/1234"},"msg":"Some important info...","time":"2014-08-19T20:59:43.542Z","v":0}
In addition to user-initiated request log events, this module will also listen for server log
events, request
events, and internalError
events, and log those if not filtered by the configured tags
or ignoredTags
.
To provide a little more context about a log message, you can log messages like so:
server.log(['mytag'], { message: 'My log message', other: 'Some other data' });
By passing an object as the second paramter, you can pass along context with your message. If no message is detected a default message will be used. Here are the default messages for the corresponding Hapi events:
log
: "Hapi Server Log"request
: "Hapi Server Request Log"
If an internalError
event is received, then the log message will be the error message.
Version Compatibility
Currently compatible with: Hapi 16.x.x
- 0.1.x - Hapi 6.x.x
- 0.2.x - Hapi 7.x.x
- 0.3.x - Hapi 8.x.x
- 0.4.x - Hapi 11.x.x
- 1.0.x - Hapi 16.x.x
- 2.0.x - Hapi 16.x.x
License
The MIT License (MIT)