[Hapi.js] Logging with good and good-console

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Hapi.js] Logging with good and good-console相关的知识,希望对你有一定的参考价值。

hapi doesn‘t ship with logging support baked in. Luckily, hapi‘s rich plugin ecosystem includes everything needed to configure logging for your application. This post will introduce good, a process monitor for hapi, and good-console, a reporter for good that outputs to standard out.

 

Install: 

npm install --save good good-console

 

‘use strict‘
var Hapi = require( ‘hapi‘ );

/**
 * set up server connection
 * @type {"hapi".Server}
 */
var server = new Hapi.Server();
server.connection( {
    host: ‘localhost‘,
    port: 8000
} );

/**
 * log
 */
var goodOptions = ({
    reporters: [
        {
            reporter: require(‘good-console‘),
            events: {log: [‘error‘], response: ‘*‘} // only log out error event
          //  events: {log: ‘*‘, response: ‘*‘} // log out any log event
        }
    ]
});

server.register({
    register: require(‘good‘),
    options: goodOptions
}, function(err){
    /**
     * Routers
     */
    server.route( {
        method: ‘GET‘,
        path: ‘/‘,
        handler: function ( request, reply ) {
            server.log(‘error‘, ‘Error happened‘); // log error to the terminal
            server.log(‘info‘, ‘replying‘); // log info to the terminal
            reply( ‘hello hapi!‘ );
        }
    } );

    server.route( {
        method: ‘GET‘,
        path: ‘/{name}‘,
        handler: function ( request, reply ) {
            reply( "hello " + request.params.name + "!" );
        }
    } );
});


/**
 * Start the server
 */
server.start( function (err) {
    if (err) {
        throw err;
    }
    console.log( ‘Started at:‘, server.info.uri )
} );

 

 

 

以上是关于[Hapi.js] Logging with good and good-console的主要内容,如果未能解决你的问题,请参考以下文章

[Hapi.js] Managing State with Cookies

[Hapi.js] Friendly error pages with extension events

[Hapi.js] Extending the request with lifecycle events

如何在 hapi.js 上的路径上使用 hapi-auth-jwt2 身份验证?

hapi.js - 404 路由 VS 静态文件路由

如何使用 dropzone.js 和 hapi.js 上传图片