2011-05-07 31 views
4

我想編寫一些可以像下面的代碼一樣使用的日誌類。是否可以編寫這樣的JavaScript日誌類?

// short-hand for logging. 
log('log message', [other parameter]); 

// full path for writing log. 
log.write('log message', [other parameter]); 

// print all log 
log.print([optional parameter]); 

此外,它必須能夠寫成流利的模式。

log.write('log message').print(); 

最後,它應該通過使用下面的代碼重置。

log = new log(); 

感謝,

+1

聽起來像一個很好的項目!但你的問題是什麼? – 2011-05-07 07:10:42

+0

我可以編寫表示類似#1的JavaScript,但不能提供像#2和#3這樣的功能。 – 2011-05-07 07:15:17

回答

3

讓我們實現它作爲一個正常的對象,然後再經過添加一些其他的語法:

var log = {}; 

log.write = function() { 
    // stuff... 
    return this; 
}; 

log.print = function() { 
    // stuff... 
    return this; 
}; 

log.reset = function() { 
    // stuff 
    return this; 
}; 

由於功能也是一個對象,它可以有屬性,因此您可以用var log = {};替代,而使用重定向到log.write的功能。

function log() { 
    return log.write.apply(log, arguments); 
} 

最後,對於自復位的語法,可以檢測到一個新的實例,但是,而不是創建一個新的對象,你重置日誌和手相同的對象回來了!

所以,現在的日誌功能看起來就像這樣:

function log() { 
    if (this instanceof log) { 
     return log.reset.apply(log, arguments); 
    } 
    return log.write.apply(log, arguments); 
} 

你可以看一下jsFiddle地看到,它的工作原理。警告:該頁面上有很多alert()!

+0

你的代碼幾乎是有效的。但它不保存日誌對象中的任何數據。所以log類的構造函數是沒用的。 – 2011-05-07 09:53:30

+0

我只是回答如何根據你的語法來調用'log'對象。你必須自己實現日誌記錄部分。 :) – Thai 2011-05-07 10:02:44

+0

你可以把像'this.data = [];''在log.reset()'然後'登錄=新的日誌()'重置爲第一次使用的對象。 – Thai 2011-05-07 10:03:44

1
var Logger = function(msg,p){ 
    this.msg = typeof msg != 'undefined' ? msg : ''; 
    this.p = typeof p != 'undefined' ? p : ''; 
} 
Logger.prototype = { 
    write : function(msg,p){ 
     this.msg = typeof msg != 'undefined' ? msg : ''; 
     this.p = typeof p != 'undefined' ? p : ''; 
    }, 
    print : function(p){ 
     this.p = typeof p == 'undefined' ? this.p : p; 
     if(this.p) 
      alert(this.msg); 
     else 
      console.log(this.msg); 
     return this; 
    }, 
    reset : function(){ 
     return new Logger(); 
    } 
} 

function log(msg,p){ 
    return new Logger(msg,p).print(); 
} 

然後你可以使用:

log("alert me",true); 
log("log me in console!"); 
log().write("a msg",false).print(); 
var l = log(); 
l.write().print().reset(); 
+0

我想要「log.write」而不是「log()。write」。這完全不是一回事。你的代碼只是簡單的類(Logger)和一些輔助函數(log)。 – 2011-05-07 08:05:01

1

console.log在Firebug服務記錄的目的是什麼?雖然,可以編寫自己的記錄器實現。但問題是何時應該使用記錄器?類似像您所提出的實現可能是在服務器端的像犀牛

運行的JavaScript有用的,但我已經寫了一些代碼,請試試這個

<html> 
<head> 

<script> 
    var log = function() {  

    var p_this = this; 


    this.write = function(p_msg){ 
     p_this.msg = p_msg; 
     return this.write; 
    }, 

    this.write.print = function(){ 
     alert(p_this.msg); 
    } 


    }; 
var log1 = new log(); 
log1.write('test_message').print(); 

</script> 

</head> 

<body> 
</body> 

</html> 

這可能是有益的。這是你正在尋找的模式的概念代碼。你必須修改或改進。您可以提供重置和所有的邏輯。

+0

你不能直接調用的console.log,警告,錯誤或其它功能的家用遊戲機,因爲它總是在IE中顯示錯誤,當你不運行的開發工具。 – 2011-05-07 08:07:36

+0

是的,我的意思是類似的功能。畢竟我們使用這些記錄器主要是爲了調試目的而不是生產日誌記錄... – zudokod 2011-05-07 08:32:15

+0

我試圖在生產中使用它來跟蹤錯誤發生時的錯誤。 – 2011-05-07 08:44:37

1

你可以試試這個光榮的大作:

var log = (function(out){ 
    var msgs = []; 

    function log(msg){ 
    if (typeof msg == 'string'){ 
    msgs.push(msg); 
    return log;} 
    else{ 
    msgs = []; 
    return log;}} 

    log.write = function(msg){ 
    log(msg); 
    return log;}; 

    log.print = function(){ 
    for(var i=0, l=msgs.length; i<l; i++){ 
     out(msgs[i]);}}; 
    return log;})(function(s){console.log(s);}); 

真正的輸出將在年底注入。你應該測試console.log是否存在,並使用其他方法(我不知道你更喜歡什麼)。

我嘗試了以下幾件事:

log('First message'); 
log.write('Second message')('Third message').write('Fourth message'); 
log.write('Fifth message').print(); 
log.print(); // Prints all messages again. 

log = new log(); // Reset. 
log('Lonely!').print(); // Prints only 'Lonely!'. 

當心:log();log(undefined);(與undefined未定義)將重置的東西爲好,而new log('Foobar');將添加消息'Foobar'。但是這不在你的測試用例中,所以我忽略了它。

這也是可能的:

(new log()).write('Message'); 
(new log())('Message'); 
(new log()).print(); // Prints empty log. 
0

嘗試看看JSLog JavaScript的日誌框架:

https://github.com/dingyonglaw/JSLog

它支持所有的瀏覽器,同時還放火,精簡版;

============== 
Example Usage: 
============== 

Logging: 
-------- 

// Register a module logger, p is now logger for 'App.Login' 
var p = JSLog.Register('App.Login'); 

// Log something, and it will display "[App.Login] something" in your console; 
p.log('something'); 

// You can do something as usual: 
p.warn('warning!'); 
p.info('info msg'); 


Log Level: 
---------- 
The Logger comes with 5 level of logging access: 1-5 
which coresponding to: 'error', 'warn', 'info', 'debug', 'log' 

// You can set your level like this, to display only info message in console 
p.SetLevel(3); 

// You can get your current logging level with: 
p.GetLevel(); 


Logging History: 
---------------- 
The logger records your logs and you can Dump it later! 

// Dump by Module Name 
JSLog.Dump('App.Login'); 

// Dump all modules in nested view 
JSLog.Dump(); 

// Selective Dump by filtering module name: 
JSLog.SetFilter('module-name'); 
JSLog.Dump(); // Will exclude 'module-name' module 

// Clear filter by module name: 
JSLog.UnsetFilter('module-name'); 

// Get assigned filters 
JSLog.GetFilter(); 
相關問題