-
第一步,引用依赖, var bunyan = require(‘bunyan’); var env = process.env.NODE_ENV || ‘development’;
-
第二步,写一个打印日志级别的js,一般放在lib/log.js var logger = { development: bunyan.createLogger({ name: “general”, streams: [ { level: ‘debug’, stream: process.stdout }, { level: ‘error’, path: ‘…/error.log’ }, { level: ‘debug’, path: ‘…/debug.log’ } ] }), staging: bunyan.createLogger({ name: “general”, streams: [ { level: ‘debug’, stream: process.stdout }, { level: ‘error’, path: ‘…/error.log’ }, { level: ‘debug’, path: ‘…/debug.log’ } ] }), production: bunyan.createLogger({ name: “general”, streams: [ // { // level: ‘debug’, // stream: process.stdout // }, { level: ‘error’, path: ‘…/error.log’ }, { level: ‘debug’, path: ‘…/debug.log’ } ] }), };
第三步.server.js中注册这个东东, var app = module.exports = loopback(); console.log(’======== Running at env:’, process.env.NODE_ENV); app.logger = require(’./lib/logger’);
- 第四步,你要使用打印日志的js中,引用:var Logger = app.logger;
- 第五步, 跟console.log();差不多Logger.log();就ok拉 下面介绍一种特殊的用法:比如你的每个log的地方,你都希望能打印一下某个id,你就可以使用如下的child功能 var log = Logger.child({device:applianceId});