PaulSpoerry.com

Social Media, technology, and geeky stuff for your brain.

  • Home
  • About
    • Privacy Policy
  • Categories
    • Google+ Posts
    • Site news
    • Tech
      • Android
      • Apple
      • Chrome
      • Gadgets
      • Hacking
      • Linux
      • OSX
      • Privacy
      • Web Life
        • Bittorrent
        • Facebook
        • FireFox
        • GMail
        • Google
        • Google+
        • Twitter
        • WordPress
        • Windows
          • Windows 7
    • Google+: Getting Started Guides
    • Games
    • Meditation
    • Politics
    • Science
    • That’s freakin hilarious
  • Code
    • FreeImageZoom
    • Post Editor for Google+™
    • The Plus Editor
  • Contact
You are here: Home / Tech / Quick console.log wrapper

Quick console.log wrapper

May 11, 2012 by Paul Spoerry 1 Comment

Lightening post! Need a simple console.log wrapper?! Read on…. 

Today I was debugging an issue for a client and needed a way to debug without breaking their site. Sadly, there was no prior testing done (despite them having a test environment) so I was debugging directly on the live site. If you’ve done any front-end web work recently you likely know about the console and console.log. Firebug, Chrome Developer Tools, and even IE now support the console. Console.log is quick and dirty debugging. I needed the ability to splash a boatload of log messages out but wanted to be able to control when they displayed. There are tons of libraries out there to do this, and of course jQuery supports it but using a third party utility (no, jQuery isn’t currently in use) wasn’t an option. So here’s a simple console.log wrapper.

var DEBUG = false; // enable console logging ex: console.log('sCoverageAsOfDate= ');
 if (!DEBUG) {
    if (!window.console) window.console = {};
    var methods = ["log", "debug", "warn", "info"];
    for (var i = 0; i < methods.length; i++) {
       console[methods[i]] = function () { };
    }
 }

Now it’s super simple to flip the switch and control if debug information will be written to the console or not. Have a great weekend!

Filed Under: Tech, Web Life Tagged With: console, li, log, read

About Paul Spoerry

I’m a groovy cat who’s into technology, Eastern Thought, and house music. I’m a proud and dedicated father to the coolest little guy on the planet (seriously, I'm NOT biased). I’m fascinated by ninjas, the Internet, and anybody who can balance objects on their nose for long periods of time.

I have a utility belt full of programming languages and a database of all my knowledge on databases... I practice code fu. Oh, I've also done actual Kung Fu, and have a black belt in Tae Kwon Do.

I run. I meditate. I dance. I blog at PaulSpoerry.com, tweet @PaulSpoerry, and I'm here on Google+.

I'm currently work for IBM developing web enabled insurance applications for IBM and support and develop a non-profit called The LittleBigFund.

Comments

  1. tanghus says

    November 15, 2012 at 5:29 pm

    Neat trick. It came in handy https://github.com/owncloud/core/pull/458
    Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2023 · Epik on Genesis Framework · WordPress · Log in