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. SqlBulkCopy使用心得 (大量数据导入)

    文章转载原地址:http://www.cnblogs.com/mobydick/archive/2011/08/28/2155983.html 最近做的项目由于之前的设计人员懒省事,不按照范式来,将一 ...

  2. node-sqlite3-API-归纳总结

    SQLITE3-API-LIST:API1. new sqlite3.Database(filename,[mode],[callback]) 返回数据库对象并且自动打开和连接数据库 它没有独立打开数 ...

  3. cocoa pods 安装 转载

    1.打开终端 终端输入  $ruby -v  查看ruby的版本 打印代码: ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64- ...

  4. PHP中PDO DEMO

    PDO => PHP DATABASE OBJECT 1.Select $dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname&quo ...

  5. jquery的Post方法$.post()

    $.post是jquery自带的一个方法,使用前需要引入jquery.js 语法:$.post(url,data,callback,type); url(必须):发送请求的地址,String类型 da ...

  6. js中的function

    Math方法详解 Math.sqrt(x)    计算X开平方 Math.sqrt(x,y)   计算xy Math.round(x)   计算x 四舍五入的值 getBoundingClientRe ...

  7. talos项目记录

    1. 跑schedule : php src/cli-schedule/cli.php -a sales.coupon.offer-coupons  所用方法在schedule里

  8. 查看Linux系统相关版本信息

    1.“uname -a” 查看电脑以及操作系统的相关信息 2.“cat /proc/version” 查看运行的内核版本 3."cat /etc/redhat-release",  ...

  9. Yii 2.0: yii2-highcharts-widget创建饼状图

    安装 The preferred way to install this extension is through composer. 项目根目录下执行: php composer.phar requ ...

  10. maven项目依赖被改为文件夹时如何改回lib

    如图