Override console.log(); for production [duplicate]

Override console.log(); for production [duplicate]

This question already has an answer here:

How to quickly and conveniently disable all console.log statements in my code?

23 answers

I’m fairly new to Javascript development so this might be a real newbie question.
I’ve got a sencha-touch application riddled with console.log(); for debugging purposes.
I’ve got chirpy doing all of my build time combining. It outputs a app.debug.js for debugging as well as a app.min.js for production
Now I could go through all of my code files looking for console.log(); and delete it manually when I’m ready to go to production, but I’m wondering if there’s a way to override the method.
Basically, whenever the console.log(); method is called, DO NOTHING.
That way, I can put the override code file in my production config, and NOT in my debug config.
Is this possible?


Solution 1:

Put this at the top of the file:

var console = {};
console.log = function(){};

For some browsers and minifiers, you may need to apply this onto the window object.

window.console = console;

Solution 2:

Or if you just want to redefine the behavior of the console (in order to add logs for example)
You can do something like that:

// define a new console
var console=(function(oldCons){
    return {
        log: function(text){
            // Your code
        info: function (text) {
            // Your code
        warn: function (text) {
            // Your code
        error: function (text) {
            // Your code

//Then redefine the old console
window.console = console;

Solution 3:

It would be super useful to be able to toggle logging in the production build. The code below turns the logger off by default.

When I need to see logs, I just type debug(true) into the console.

var consoleHolder = console;
function debug(bool){
        consoleHolder = console;
        console = {};
            console[key] = function(){};
        console = consoleHolder;

To be thorough, this overrides ALL of the console methods, not just console.log.

Solution 4:

console.log = function(){};

Override it like any other thing.

Solution 5:

I use something similar to what posit labs does. Save the console in a closure and you have it all in one portable function.

var GlobalDebug = (function () {
    var savedConsole = console;
    return function(debugOn,suppressAll){
        var suppress = suppressAll || false;
        if (debugOn === false) {
            console = {};
            console.log = function () { };
            if(suppress) {
                console.info = function () { };
                console.warn = function () { };
                console.error = function () { };
            } else {
                console.info = savedConsole.info;
                console.warn = savedConsole.warn;
                console.error = savedConsole.error;              
        } else {
            console = savedConsole;

Just do globalDebug(false) to toggle log messages off or globalDebug(false,true) to remove all console messages.

Solution 6:

I would recommend using: https://github.com/sunnykgupta/jsLogger


  1. It safely overrides the console.log.
  2. Takes care if the console is not available (oh yes, you need to factor that too.)
  3. Stores all logs (even if they are suppressed) for later retrieval.
  4. Handles major console functions like log, warn, error, info.

Is open for modifications and will be updated whenever new suggestions come up.

Disclaimer: I am the author of the plugin.