Hapijs – Built in methods

Hapi has some built in logging methods, as well as some limited capability for viewing these logs.

There are two nearly identical logging methods,
– server.log,
– request.log.
> They both accept up to three parameters. They are, in order, tags, data, and timestamp.

– tags is a string or array of strings used to briefly identify the event. Think of them like log levels, but far more expressive. For example, you could tag an error retrieving data from your database like the following:

> server.log([‘error’, ‘database’, ‘read’]);

– Any log events that Hapi generates internally will always have the Hapi tag associated with them.

– The second parameter, data, is an optional string or object to log the event. This is where you would pass in things like an error message, or any other details that you wish to go along with your tags. The log event will automatically have the URI of the server it’s associated with as a property.

– Last is the timestamp parameter. This defaults to Date.now(), and should only be passed in if you need to override the default for some reason.

Retrieving request logs

– Hapi also provides a method (request.getLog) to retrieve log events from a request, assuming you still have access to the request object. If called with no parameters it will return an array of all log events associated with the request. You may also pass a tag or array of tags to filter the result set. This can be useful for retrieving a history of all logged events on a request when an error occurs for analysis. Configuration

Configuration

By default, the only errors Hapi will print to console are uncaught errors in external code and runtime errors from incorrect implementation of Hapi’s API. You can configure your server to print request events based on tag, however.
– For example, if you wanted to print any error in a request you would configure your server as follows:

[cc lang = “javascript”]
var server = new Hapi.Server({
debug:
{
request: [‘error’]
}

});

[/cc]

Logging plugins

– The built-in methods are fairly minimal, however, and for more thorough logging you should really look into using a plugin like good or bucker.


Posted

in

, , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *