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
?
2 Answers
Reset to default 5You 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条)