2016-03-18 6 views
0

初心者はこちらです。急行3.2.6では高速ロギングカラーフォーマット

私はそうのように、(ダークモード?)グレーアウトし、目に優しいあなたが標準出力にapp.use(express.logger('dev'))へのログの形式をロギングを設定するとされていることに気づい:

enter image description here

しかし

、エクスプレス4.0を使用してapp.js

var morgan = require('morgan'); 
app.use(morgan('dev')); 

に以下でログインするモーガンを用いて端末にロギングこの

よう判明します

とにかく、express 4.0とmorganを使用してログを記録するための「ダークモード」テーマを取得するには?または、これはExpress 3.0で利用可能ですか?

これを行うにはこれを変更することができますか? https://github.com/expressjs/morgan/blob/master/index.js#L183

答えて

0

まあカスタムロガーがこの

//logger.js 
require('colors'); 
var _ = require('lodash'); 
var config = require('../config/config'); 

// create a noop (no operation) function for when loggin is disabled 
var noop = function() { 
}; 
// check if loggin is enabled in the config 
// if it is, then use console.log 
// if not then noop 
var consoleLog = config.logging ? console.log.bind(console) : noop; 

var logger = { 
    log: function() { 
    var tag = '[ ✨ LOG ✨ ]'.green; 
    // arguments is an array like object with all the passed 
    // in arguments to this function 
    var args = _.toArray(arguments) 
     .map(function (arg) { 
      if (typeof arg === 'object') { 
      // turn the object to a string so we 
      // can log all the properties and color it 
      var string = JSON.stringify(arg, null, 2); 
      return tag + ' ' + string.cyan; 
      } else { 
      return tag + ' ' + arg.cyan; 
      } 
     }); 

    // call either console.log or noop here 
    // with the console object as the context 
    // and the new colored args :) 
    consoleLog.apply(console, args); 
    }, 

    error: function() { 
    var args = _.toArray(arguments) 
     .map(function (arg) { 
      arg = arg.stack || arg; 
      var name = arg.name || '[ ❌ ERROR ❌ ]'; 
      var log = name.yellow + ' ' + arg.red; 
      return log; 
     }); 

    consoleLog.apply(console, args); 
    } 
}; 

module.exports = logger; 

使い方

//controller 
var logger = require('./relative/path/to/logger'); 
logger.log('some thing'); 
logger.error('some thing'); 
// you may use custom colors and create custom functions like warning() etc 

を試みることによって、あなたのロガーの色をカスタマイズすることができ、それが役に立てば幸い;)