Forget console.log

I remember the day I learned that I could use console.log to test my app instead of having to alert everything. Man did I feel good and smart. Everything was very smooth, and fast. No longer did I have to close every single alert in order to get to the next one, nor did I had to worry about leaving some rouge alert saying You made it into the if!

But then again every time I tried to test in IE beauty became the beast. Instead of getting a good log in the IE console which… ooops doesnt exist, I got a nasty js error saing what the hell is console.log ?

So I got into the habit of cleaning up my console.logs after using them which really didn’t help me testing out something in staging or dev because when you are making heavy client side apps, everything is ajax based, and you always need to know if you are showing the exact data that you received or if your js code is doing something funky.

Solution simply use another function that checks if console.log exists and if so log it, else don’t


function cLog( text ){
if( (window['console'] !== undefined) ){
console.log( text );
}
}

Updated with a better solution

if(!this.console){
window.console={
log:function(){}
};
}

— Update as of sep 28 2014,

Using a grunt task to remove all your logs helps prevent deployment of logs, and avoid IE9 errors.

https://www.npmjs.org/package/grunt-remove-logging

Enjoy!

14 thoughts on “Forget console.log

    1. Great idea Alex!
      I guess you could put and absolute positioned element at the bottom of the screen to simulate as much as possible a typical console.log
      Then all you have to do is remove it from the DOM once you are finished. jQuery shouldn’t fire an error if the DOM element does not exist I believe.
      Or if we are already removing the div might as well remove the DOM console.log to I guess. But yeah this should work.

  1. Works Beautifully! Just read this post, in middle of a project, and was facing this problem.

  2. I like the helpful information you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.
    I’m quite sure I’ll learn a lot of new stuff right here!

    Good luck for the next!

  3. When someone writes an piece of writing he/she keeps the thought of a user
    in his/her brain that how a user can be aware of it. So that’s why this article
    is perfect. Thanks!

  4. whoah this blog is excellent i like reading your posts.
    Stay up the great work! You already know, many persons are huntung round for this
    information, you can help them greatly.

  5. I hardly drop comments, but i did some searching and wwound up here
    Forget console.log | Digital Caveman. And I actually do have a few
    questions for you if you do not mind. Could it be just me or does it look like some of the comments look like they are
    written by brain dead people? 😛 And, if you are posting att other online
    sites, I’d like to keep up with everything new you have
    to post. Would you make a liszt of every one of your communal sites like
    your twitter feed, Facebook page or linkedin profile?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s