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的更多相关文章

  1. log4j报错ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

    ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only err ...

  2. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  3. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  4. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  5. [Hapi.js] Friendly error pages with extension events

    hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...

  6. [Hapi.js] Extending the request with lifecycle events

    Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...

  7. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  8. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  9. Js调试中不得不知的Console

    在js调试中,大部分的前端人员都是采用console.log()方法来打印出调试的数据,但是很多人都不知道console这个对象有很多很实在的方法,本文就来介绍一下这些方法的使用. 一.console ...

随机推荐

  1. BZOJ4195 NOI2015 程序自动分析

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Description 在实现程序自动分析的过程中,常常需要判定一些约束条件 ...

  2. hdu 2191多重背包

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. hdu5344 MZL's xor(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud MZL's xor Time Limit: 2000/1000 MS (Java/ ...

  4. redis的特色

    总结一下redis的特点: 1.独特的键值对模型 很多数据库只能处理一种数据结构:     • SQL 数据库 —— 表格     • Memcached —— 键值对数据库,键和值都是字符串     ...

  5. winform textbox 的自动实现功能

    好久没写博客了,主要是太懒了,之前因为做bs的比较多现在想转cs端了,虽然现在做cs也一年了,可接触的东西太过零碎了,以至于感觉这一年好像什么都没有学到.估计是因为学了之后没有记录,不扎实,然后又忘记 ...

  6. chart画图

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  7. c# 输出excel、word实例只需一行代码

    第一.首先下载 WindICFrameNet.dll 下载地址:http://pan.baidu.com/s/1dDBqC9r 第二.前台代码 <div style="display: ...

  8. android获取sdk更新

    http://www.th7.cn/Program/Android/201310/154981.shtml 网上许多解决 Android SDK Manager 无法更新(下载)的问题的方法基本都是将 ...

  9. iPhone 和Android应用,特殊的链接:打电话,短信,email;

    http://ice-k.iteye.com/blog/1426526 下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这 ...

  10. Linux文件夹执行权限

    在Linux中,文件有三种权限--可读,可写,可执行.目录也有三种权限--可读,可写,可执行.但是实际上他们有着不同的意义. 对于文件: 可读 :表示可以读取文件里的数据: 可写 :表示可以改变和删除 ...