Is it possible to get a better result for `goog.debug.Logger` (for example,` console.log`)?

The google-clos library also contains a logging system that should be familiar to most developers. It's cute. Unfortunately, the output you get is less expressive than when using console.log, as was done by some browsers / plugins.

For example, if you write console.log(window)in Chrome, the console displays an object that you can interactively check. When using the google-close registrar, it will not do this. I assume that it will internally just pass the string representation of your object to console.log. Thus, you lose a lot of convenience.

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

Alternatively, this can be prevented by first checking for existence, 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 structure, it would be nice to be able to use it. As of now, I find it console.logmuch more useful from time to time.

So my question is (tl / dr): can I make a Google close user console.log(x)when I write myLogger.info(x)instead using a string representation x?

+5
source share
2 answers

goog.debug.FancyWindow, . - Google : https://github.com/google/closure-library/blob/master/closure/goog/demos/debug.html .

, , , ... , :

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

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

.

,

+5

Google Closure . . . .

 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));
+2

All Articles