Whisher
Whisher

Reputation: 32766

Hapi good how to print debug message without using console log

I'm using hapi and good module and I'm wondering what's the best way to print my debug message without use console log.

For instance now is

server.register(
    {
            register: require('good'),
            options: options
    },
    function (err) {
        if (err) {
                    return console.error(err);
            }
            // If the module is required by another script, we don’t start the server (ie test suite)
            if (!module.parent) {
                server.start(function () {
                console.info('Server started at ' + server.info.uri);
                    });
        }

    }
);

is it good using https://github.com/visionmedia/debug like

server.register(
    {
            register: require('good'),
            options: options
    },
    function (err) {
        if (err) {
                    return console.error(err);
            }
            // If the module is required by another script, we don’t start the server (ie test suite)
            if (!module.parent) {
                server.start(function () {
                    debug('Server started at ' + server.info.uri);
                });
        }

    }
);

or there is a better options ?

Upvotes: 1

Views: 4463

Answers (1)

Matt Harrison
Matt Harrison

Reputation: 13587

Debug is pretty good. Or, if you want to stay inside the hapi ecosystem, you can use Good to manage your logging needs:

var Hapi = require('hapi');

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

var options = {
    reporters: [{
        reporter: require('good-console'),    // Log everything to console
        events: { log: '*' }
    }, {
        reporter: require('good-file'),       // Log 'debug' to debug_log.log
        events: { log: 'debug' },
        config: 'debug_log.log'
    }, {
        reporter: require('good-file'),       // Log 'error' to error_log.log
        events: { log: 'error' },
        config: 'error_log.log'
    }]
};

server.register({
    register: require('good'),
    options: options
}, function (err) {

    if (err) {
        server.log(['error'], err);             // log an 'error' message
    }

    server.start(function (err) {

        if (err) {
            server.log(['error'], err);         // log an 'error' message
        }

        server.log(['debug'], 'Started...');    // log a 'debug' message
    });
});

Aside from logging to console there's a bunch of other "reporters" that let you log to HTTP, UDP etc:

https://github.com/hapijs/good#officially-supported-by-hapijs

Upvotes: 5

Related Questions