Is it possible to write a JavaScript log class like this?

I want to write some journal class that can be used as the following code.

// 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]); 

In addition, it should be written as a smooth drawing.

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

Finally, you need to reset it using the following code.

 log = new log(); 

Thanks,

+4
source share
5 answers

First implement it as a regular object, then add other syntaxes after:

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

Since the function is also an object, it can have properties, so you can replace var log = {}; to a function that redirects to log.write .

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

Finally, for the self-reset syntax, you can find a new instance, but instead of creating a new object, you reset register and pass the same object!

So, now the log function will look like this:

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

You can look at jsFiddle to see that it works. Warning: many warnings () on this page!

+3
source
 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(); } 

And then you can use:

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

Does console.log not work in firebug for logging? Although, you can write your own registrar implementation. But the question is, when should the recorder be used? A similar implementation, as you suggested, may be useful in server-side javascript, such as Rhino

But I wrote some code, try this

 <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> 

This may be helpful. This concept code for the u template is looking for. You must change it or improve it. You can specify reset logic and that’s it.

+1
source

You can try this magnificent masterpiece:

 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);}); 

The actual output function is entered at the end. You should verify that console.log exists, and use the alternative otherwise (I don't know what you prefer).

I have tried the following things:

 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!'. 

Beware: log(); or log(undefined); (with undefined undefined) will reset the thing, and new log('Foobar'); will add the message 'Foobar' . But this was not in your test cases, so I ignored it.

It is also possible:

 (new log()).write('Message'); (new log())('Message'); (new log()).print(); // Prints empty log. 
+1
source

Try a look at the javascript javascript framework:

https://github.com/dingyonglaw/JSLog

It supports all browsers, as well as firebug-lite;

 ============== 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(); 
0
source

All Articles