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

The google-closure library also contains a logging system which should be familiar to most developers.

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?

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?

Share Improve this question edited Feb 25, 2015 at 16:17 CommunityBot 11 silver badge asked Jun 25, 2012 at 9:31 exhumaexhuma 21.8k15 gold badges96 silver badges138 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744859881a4597637.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信