最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: console logging - Stack Overflow

programmeradmin1浏览0评论

I use console.log in my JS files to trace the application.

The problem: logs are in production environment.
How can I remove lines like console.log from code?

P.S. Please do not advice text solutions like find + xargs + grep -v.

I use console.log in my JS files to trace the application.

The problem: logs are in production environment.
How can I remove lines like console.log from code?

P.S. Please do not advice text solutions like find + xargs + grep -v.

Share Improve this question asked Aug 25, 2012 at 20:45 DmitryDmitry 7,59312 gold badges61 silver badges86 bronze badges 3
  • 2 You want to remove the lines from your code, or you don't want console output to be printed? – cheeken Commented Aug 25, 2012 at 20:48
  • Note that there are other methods as well (console.dir for example). – Salman Arshad Commented Aug 25, 2012 at 20:51
  • I think, it's better to remove these lines from code. – Dmitry Commented Aug 25, 2012 at 21:20
Add a ment  | 

4 Answers 4

Reset to default 4

For my significant projects, I have my own logging function that internally uses console.log(), but there are no console.log() calls in my code except for the one place in this function. I can then enable or disable logging by changing one variable.

My function is actually a little more involved than this with options to put the output into places other than just the console, but conceptually, it looks like this:

// change this variable to false to globally turn off all logging
var myLoggingEnabled = true;   

function myLog() {
    if (myLoggingEnabled) {
        if (window.console && console.log) {
            console.log.apply(this, arguments);
        }
    }
}

You can then use code like this to log:

myLog(foo);

FYI, for deployed code pactness and performance optimization, I also have a minimization step that removes all calls to myLog() from my code. This is an optimization that I've chosen to take advantage of. Perhaps you could share why you wouldn't also consider this type of optimization.

Well, you can disable them with

console.log=function(){}

But the lines will be there unsless you delete them manually.

If you use Grunt you can add a task so as to remove/ment the console.log statements. Therefore the console.log are no longer called.

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

Yeah, I had a similar situation, I posted about it here. http://bhavinsurela./naive-way-of-overriding-console-log/ This is the gist of the code.

var domainNames =["fiddle.jshell"]; // we replace this by our production domain.

var logger = {
    force:false,
    original:null,
    log:function(obj)
    {
        var hostName = window.location.hostname;
        if(domainNames.indexOf(hostName) > -1)
        {
            if(window.myLogger.force === true)
            {
                window.myLogger.original.apply(this,arguments);
            }
        }else {
            window.myLogger.original.apply(this,arguments);
        }
    },
    forceLogging:function(force){
        window.myLogger.force = force;
    },
    original:function(){
        return window.myLogger.original;
    },
    init:function(){
        window.myLogger.original = console.log;
        console.log = window.myLogger.log;
    }
}

window.myLogger = logger;
console.log("this should print like normal");
window.myLogger.init();
console.log("this should not print");
window.myLogger.forceLogging(true);
console.log("this should print now");
发布评论

评论列表(0)

  1. 暂无评论