23

I'd like to see a nice log with short info about each request to my server, for use during development. I've seen the documentation on http://hapijs.com/api#request-logs, but I couldn't understand enough of it to get it working.

What should I pass as the config object when I create the server? Should I then listen to events and log them or does it happen automatically? How do I log all the requests, and not just the errors?

I'd like to avoid installing logging libraries.

jcollum
  • 43,623
  • 55
  • 191
  • 321
mik01aj
  • 11,928
  • 15
  • 76
  • 119
  • did you try to use a plugin like Good or Bucker as suggested in [http://hapijs.com/tutorials/logging](http://hapijs.com/tutorials/logging)? – mucio Apr 21 '15 at 14:34
  • I've seen this, but I'd rather not introduce a new library unless it it really necessary. I want a plain and simple log of all requests, like most libraries do by default. Does Hapi really require a plugin for this? – mik01aj Apr 22 '15 at 06:57
  • Good is deprecated. "Note: this module is being deprecated on December 31st, 2020 due to lack to available support resources. Please consider using another logging plugin." – dykstrad May 17 '21 at 22:41

3 Answers3

51

So I found a way:

server.events.on('response', function (request) {
    console.log(request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.path + ' --> ' + request.response.statusCode);
});

The log then looks like this:

127.0.0.1: GET /myEndpoint/1324134?foo=bar --> 200
127.0.0.1: GET /sgsdfgsdrh --> 404

Answer edited for Hapi v18, see older versions here

mik01aj
  • 11,928
  • 15
  • 76
  • 119
  • 1
    You should use [`Good`](https://github.com/hapijs/good) or other [logging](http://hapijs.com/tutorials/logging) plugin. – paulodiovani Aug 03 '16 at 16:26
  • 15
    No-no-no. I already wrote in the question that I don't want logging libraries. And I repeated this in comments to other answer. I just needed a *simple* logging for dev use. `good` might be an answer to *your* question, but it's not an answer to *my* question as I asked it. – mik01aj Aug 04 '16 at 08:09
  • `request.response.source` gets you the response body – Cory Mawhorter Jan 11 '18 at 23:54
  • 3
    as of hapi v17, it's `server.events.on()` rather than 'server.on()` – nxmohamad Oct 25 '18 at 17:53
  • in Hapi v18, use either `request.url.pathname` or `request.path`. – M Falanga Mar 17 '19 at 12:22
  • Good is deprecated. "Note: this module is being deprecated on December 31st, 2020 due to lack to available support resources. Please consider using another logging plugin." – dykstrad May 17 '21 at 22:41
12

In Hapi.js version above v17, please make the below changes to make it work:

server.events.on('response', function (request) {
    // you can use request.log or server.log it's depends
    server.log(request.info.remoteAddress + ': ' + request.method.toUpperCase() + ' ' + request.url.path + ' --> ' + request.response.statusCode);
});

The log will be:

127.0.0.1: GET /todo --> 200
Doron Segal
  • 2,242
  • 23
  • 22
Vipin
  • 847
  • 1
  • 10
  • 21
10

The easiest would be to use the good module with one of the good reporters, for example good-file. Here is an example how to use it:

var Hapi = require('hapi');
var Good = require('good');

var server = new Hapi.Server();
server.connection({ port: 8080 });

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {

        reply('Hello, world!');
    }
});

server.route({
    method: 'GET',
    path: '/{name}',
    handler: function (request, reply) {

        reply('Hello, ' + encodeURIComponent(request.params.name) + '!');
    }
});

server.register({
    register: Good,
    options: {
        reporters: [{
            reporter: require('good-file'),
            events: {
                response: '*',
                log: '*'
            },
            config: {
                path: '/var/log/hapi',
                rotate: 'daily'
            }
        }]
    }
}, function (err) {

    if (err) {
        throw err; // something bad happened loading the plugin
    }

    server.start(function () {

        server.log('info', 'Server running at: ' + server.info.uri);
    });
});
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • I'd rather not add another library for the project just for a simple log of all requests. – mik01aj Apr 23 '15 at 09:26
  • 1
    Note that you also [can't customize the output format with good](https://github.com/hapijs/good-file/issues/66). – mik01aj Jan 27 '16 at 13:01
  • The accepted answer is *easier* than this, for one because you don't have to worry about plugin version incompatibility. – Brent Bradburn Jan 17 '18 at 02:42
  • 2
    Good is deprecated. "Note: this module is being deprecated on December 31st, 2020 due to lack to available support resources. Please consider using another logging plugin." – dykstrad May 17 '21 at 22:41