Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get better output for `goog.debug.Logger` (like `console.log`)?

The google-closure library also contains a logging system which should be familiar to most developers. This is nice. Unfortunately, the output you get from is is less expressive as when using console.log as made available by some browsers/plugins.

For example, if you write console.log(window) in Chrome, the console will display an object which you can interactively inspect. When using the google-closure logger it will not do that. I assume that it will internally simply pass a string-representation of your object to console.log. So you lose a lot of convenience.

Because of this, I still continue to use console.log. But then, if by bad-luck, you forget to remove it from production code, your code will break in browsers which do not have console.log (f.ex.: IE).

Alternatively, it is possible to guard against this, by checking for the existence first, for example:

window.console && window.console.log && console.log(...)

or:

if (DEBUG) {
    console.log(...)
}

But both solutions are far from perfect. And, given that the library has a logging framework, it would be nice to be able to use it. As it is right now, I find console.log much more useful at times.

So my question (the tl/dr): Can I make google-closure user console.log(x) when I write myLogger.info(x) instead of it using a string-representation of x?

like image 581
exhuma Avatar asked Jun 25 '12 09:06

exhuma


People also ask

What can I use instead of console log?

You can use console. time() and it's friends console. timeStart() , console. timeEnd() , and console.

Does console log reduce performance?

First, draw a conclusion to the above problem: console. log does affect web page performance.

Why we should not use console log?

Untrustworthy Information You cannot always trust information reported by console. log() because there is simply no standardized behavior about it. You don't really know what happens under the hood. Most of the time, calling console.

Does console log affect performance node?

We already know that logging is affecting performance, especially when we use console. log because its a syncronous process. In nodejs, we have to use asyncronous process to get the best performance.


2 Answers

You can also use goog.debug.FancyWindow to have a separate window to show logging. See for more information the google closure demo page: https://github.com/google/closure-library/blob/master/closure/goog/demos/debug.html and look at the source code.

Another advantage if you're just using the console logging is that the framework will automatically prepend the module name and the time... Just add the folowing lines to use the console logging:

goog.require('goog.debug.Console');

if (goog.DEBUG) {
    debugConsole = new goog.debug.Console;
    debugConsole.setCapturing(true);
}

This will also prevent showing console logging in production code.

Regards,

Rene

like image 160
rzeldent Avatar answered Oct 12 '22 23:10

rzeldent


Google Closure can provide the information as per your need. With function, without functions of object. Refer to below code snippet.

 goog.require('goog.debug');
 goog.require('goog.debug.Logger');

 var theLogger = goog.debug.Logger.getLogger('demo');
 theLogger.info('Logging examples');

 // Create a simple object.
 var someone = {
     'name': 'peder',
         'age': 33,
         'gender': 'm',
         'kids': ['hari', 'sam', 'sneha']
 };

 // Show the object, note that it will output '[object Object]'.
 theLogger.info(someone);

 // Use expose to walk through the object and show all data.
 theLogger.info('Person: ' + goog.debug.expose(someone));


 // Does not show the functions by default.
 theLogger.info('expose (no functions): ' + goog.debug.expose(yourObject));


 // Shows the functions as well.
 theLogger.info('expose (w/functions): ' + goog.debug.expose(yourObject, true));

 // Show deepExpose, which walks recursively through data.
 theLogger.info('deepExpose (no functions): ' + goog.debug.deepExpose(yourObject));

 theLogger.info('deepExpose (w/functions): ' + goog.debug.deepExpose(yourObject, true));
like image 28
gokul Avatar answered Oct 13 '22 00:10

gokul