zoukankan      html  css  js  c++  java
  • [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 )
    } );
     
  • 相关阅读:
    shell——变量
    xxx is not in the sudoers file.This incident will be reported.的解决方法
    百度面试回忆
    iOS网络协议 HTTP/TCP/IP浅析
    使用xib封装一个自定义view的步骤
    修改了系统自带头文件后,Xcode会报错
    字典转模型规范化
    文本属性Attributes
    苹果API常用英语名词
    命令行 -- 命令"%cd%"
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5223990.html
Copyright © 2011-2022 走看看