Logging behavior changed when issue #2326 was merged, as the stack trace is no longer printed when you redirect to an error page.
In the sample code for for the reply interface there is no indication that you must log the error yourself in order to see it. Hapi no longer does that for you when you reply with an error page.
In the sample below, there is no error logged unless you comment in the console.log statement.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
server.ext('onPreResponse', function(request, next) {
var response = request.response;
//console.log('error', response.stack);
var statusCode = response.output.statusCode;
next('error').code(statusCode);
});
server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
reply.this.willCauseError('hello world');
}
});
server.start();
I am not sure if this is a bug that was introduced or if it's just a matter of documenting the new behavior. I'll be happy to update the docs if that's all that is required
The previous behavior was a bug. It should only log errors that are sent back to the client, not every error along the way. If you want those, you should use the 'request' event and filter on the 'error' tag.
Updating the docs to clarify this would be great.
@hueniverse When trying to listen to the request event and filtering on the error tag, as described, nothing is triggered. However, using request-internal and filtering on error tags does. I guess it makes sense since, according to the docs request.log() triggers the event:
request- events generated byrequest.log(). Does not include any internally generated events.
For anyone else trying to capture/log errors that don't make it to the client the following seems to work in Hapi v13:
server.on('request-internal', (req, event, tags) => {
if (!tags.error || !event.data || !event.data.data) return;
const dta = event.data.data;
if (dta.output) req.log(event.tags, 'ERROR: HTTP ' + dta.output.statusCode + ': ' + ((dta.output.payload && dta.output.payload.message) || ''));
req.log(event.tags, dta.stack);
});
Most helpful comment
@hueniverse When trying to listen to the request event and filtering on the
errortag, as described, nothing is triggered. However, usingrequest-internaland filtering onerrortags does. I guess it makes sense since, according to the docsrequest.log()triggers the event:For anyone else trying to capture/log errors that don't make it to the client the following seems to work in Hapi v13: