I have a utility function that wraps console.log with a condition, so we only call console.log if we're in the dev environment and console.log exists:
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(message);
}
};
}());
This has worked very well for normal console logs. But I've recently discovered the joys of passing more than one argument to console.log: it allows you to prefix a console log with a string, so console.log('DEBUG', object)
outputs the string plus an expandable object whose properties you can inspect. How can I change my conlog function to do this? I've tried logging out all arguments like this:
metro.conlog = (function () {
return function (message) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(arguments);
}
};
}());
But this outputs the arguments as an array, instead of the neat line you get with console.log. You can see the difference in this screenshot:
Can anybody tell me how I can reproduce the original log output?
When we want to print a message containing multiple variables along with a message, we can format console. log() accordingly by: Using specifiers like %s, %i, %f etc.
js, console. log() method is used to display the message on the console. By default, the console. log() method prints on console with trailing newline.
when the browser console is closed, calling console. log is about 10 000 times slower than calling an empty function, and when the console is open, calling it is as much as 100 000 times slower.
log method, and its friends console. warn and console. error , lets you dump objects in the console. The only difference between these functions is their “type” classification, which looks slightly different and can be filtered when viewing the console output.
Of course you can do it, this is a demo of how to do exactly what you need, with extra options added.
And the code is below:
var mylog = (function () {
return {
log: function() {
var args = Array.prototype.slice.call(arguments);
console.log.apply(console, args);
},
warn: function() {
var args = Array.prototype.slice.call(arguments);
console.warn.apply(console, args);
},
error: function() {
var args = Array.prototype.slice.call(arguments);
console.error.apply(console, args);
}
}
}());
var name = "Alex";
var arr = [1, 2, 3];
var obj = { a:1, b:2, c:3 };
var hello = function(msg){alert(msg);};
mylog.log("Name: ", name);
mylog.log("Window Debug: ", window);
mylog.error("Some error happened");
mylog.warn("Ahh... Warning", arr, obj);
mylog.log("more parameters: ", arr, obj, hello);
Try something like this
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message, object) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(message, object);
}
};
}());
where message
is something like "DEBUG" and object
is whatever object you want to examine.
If you want to be able to pass an arbitrary number of arguments into console.log
, I would suggest using the arguments
variable.
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message, object) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(arguments);
}
};
}());
As mentioned in my comments, I am unsure which browsers fully support this (I'm looking at you IE).
I have tested and confirmed that it works in current Chrome, FireFox and Safari.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With