2016-01-21 28 views
2

我正在爲我的項目尋找更好的日誌/調試方法。所以我想出了使用自定義指令的想法,比如'use strict'。定製「使用嚴格」的指令

是否有可能寫出這樣的事情

function xyz() { 
    'loglevel: info'; 
    /// Some other code 
    logging.debug("foobar"); 
} 

如果XYZ具有一個指令,記錄等級> =信息的logging.debug將無法登錄的消息。

這可能嗎?

+0

可能可以與一些令人費解的黑客,但*很多*簡單的解決方案將只是'記錄。 level ='info';'。 – JJJ

+0

這是不可能的,除非你編寫一個自定義預處理器來解析文件並替換你的日誌語句。 –

+0

我認爲你正在尋找「裝飾者」。 – 2016-01-21 12:54:51

回答

2

不,你不能創建類似的指令沒有一些真正的hackery在轉換當前函數爲字符串和檢查指令。這是不值得的時間來檢查。然而,你可以使用函數裝飾器來執行相同的功能,但是要讓你的頭腦變得有點棘手,但是一旦你做到了,就會非常強大。

我應該提到es7將會更容易實現裝飾器。它們仍然以相同的方式創建。它們是一個函數,它返回一個函數來代替原始函數。但他們有糖,例如。

對不起,我無法停下來,所以走了很遠。但現在這是一個非常完整的例子。

@logLevel('warn') 
function xyz(){ 
    // do some stuff 
} 

@logLevelInfo 
function abc(){ 
    // do some stuff 
} 

// if this is false the logging will not occur 
 
var __debug__ = true; 
 
var __debug_levels__ = ['error', 'warn']; 
 

 
// decorator to create a log level function. this is a function 
 
// that takes the log type, that returns a function that takes the 
 
// function you want to decorate with the logging functionality 
 
// that returns the decorated function that you call as xyz(...arguments). 
 
function logLevel(type) { 
 
    return function logger(fn) { 
 
    return function() { 
 
     // save time if __debug__ is false 
 
     if(__debug__){ 
 
     // run the decorated function and get the result 
 
     // may as well wrap it in a try catch in case there are any errors 
 
     try { 
 
      var result = fn.apply(this, arguments); 
 
     } catch(e){ 
 
      console.error(e); 
 
     } 
 
     if(__debug_levels__.indexOf(type) > -1){ 
 
      // log the result to the console or whatever functionality you require 
 
      console[ type || 'log' ](result); 
 
     } 
 
     // return the result so you can do something with the result 
 
     return result; 
 
     } 
 
     return fn.apply(this, arguments); 
 
    } 
 
    } 
 
} 
 

 
// this will return the first function that takes the function to decorate 
 
var logLevelInfo = logLevel('warn'); 
 
var logLevelDebug = logLevel('error'); 
 

 

 
// here we are using the decorators to wrap the original function 
 
var xyz = logLevelInfo(function xyz(arg) { 
 
    return arg + 'bar'; 
 
}); 
 

 
// same here but we are using the other decorator 
 
var abc = logLevelDebug(function abc(arg){ 
 
    return arg + 'baz'; 
 
}); 
 

 
// these functions have been decorated to perform the logging 
 
// functionality on the returned result 
 
xyz('foo'); //=> 'foobar' 
 
abc('foo'); //=> 'foobaz'
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

+0

值得一提的是,裝飾者在Babel中有適當的預設。 – 2016-01-21 13:24:11

-2
function Log(level) 
{ 
    var levels = { 'debug': 10, 'info': 20}; 

    function write(lvl) 
    { 
     var handle = function(msg) 
     { 
      if (levels[lvl] <= levels[level]) 
       console.log(lvl + ': ' + msg); 
     }; 

     return handle; 
    } 

    for (var i in levels) 
    { 
     this[i] = write(i); 
    } 
} 

var log1 = new Log('info'); 
log1.info('hello'); // will result with an output 
log1.debug('hello'); // still has an output output 

var log2 = new Log('debug'); 
log2.info('hello'); // no output here