admin管理员组

文章数量:1429485

I am using console.log() and console.dir() statements in 3 places in my 780 lines of code JS script. But all of them are useful for debugging and discovering problems that might appear when using the application.

I have a function that prints internal application's state i.e current value of variables:

printData: function () {
        var props = {
            operation: this.operation,
            operand: this.operand,
            operandStr: this.operandStr,
            memory: this.memory,
            result: this.result,
            digitsField: this.digitsField,
            dgField: this.dgField,
            operationField: this.operationField,
            opField: this.opField
        };
        console.dir(props);
    }

I also have a list of immutable "contants" which are hidden with closure but I can print them with accessor method called list(); in console. Something like this:

list: function () {
    var index = 0,
    newStr = "",
    constant = '';

    for (constant in constants) {
        if (constants.hasOwnProperty(constant)) {
            index = constant.indexOf('_');
            newStr = constant.substr(index + 1);
            console.log(newStr + ": " + constants[constant]);
         }
    }
}

The third place where I use console in debugging purposes is in my init(); function, where I print exception error if it happens.

init: function (config) {
    try {
        this.memoryLabelField = global.getElementById(MEMORY_LABEL);
        this.digitsField = global.getElementById(DIGITS_FIELD);
        this.digitsField.value = '0';
        this.operationField = global.getElementById(OPERATION_FIELD);
        this.operationField.value = '';
        return this;
    } catch (error) {
        console.log(error.message);
        return error.message;
    }
}

As my question states, should I keep these console statements in production code?

But they e very useful for later maintenance of the code.

Let me know your thoughts.

I am using console.log() and console.dir() statements in 3 places in my 780 lines of code JS script. But all of them are useful for debugging and discovering problems that might appear when using the application.

I have a function that prints internal application's state i.e current value of variables:

printData: function () {
        var props = {
            operation: this.operation,
            operand: this.operand,
            operandStr: this.operandStr,
            memory: this.memory,
            result: this.result,
            digitsField: this.digitsField,
            dgField: this.dgField,
            operationField: this.operationField,
            opField: this.opField
        };
        console.dir(props);
    }

I also have a list of immutable "contants" which are hidden with closure but I can print them with accessor method called list(); in console. Something like this:

list: function () {
    var index = 0,
    newStr = "",
    constant = '';

    for (constant in constants) {
        if (constants.hasOwnProperty(constant)) {
            index = constant.indexOf('_');
            newStr = constant.substr(index + 1);
            console.log(newStr + ": " + constants[constant]);
         }
    }
}

The third place where I use console in debugging purposes is in my init(); function, where I print exception error if it happens.

init: function (config) {
    try {
        this.memoryLabelField = global.getElementById(MEMORY_LABEL);
        this.digitsField = global.getElementById(DIGITS_FIELD);
        this.digitsField.value = '0';
        this.operationField = global.getElementById(OPERATION_FIELD);
        this.operationField.value = '';
        return this;
    } catch (error) {
        console.log(error.message);
        return error.message;
    }
}

As my question states, should I keep these console statements in production code?

But they e very useful for later maintenance of the code.

Let me know your thoughts.

Share Improve this question asked Jan 28, 2015 at 10:54 VladVlad 2,7737 gold badges52 silver badges106 bronze badges 3
  • 4 Useful or not, keeping console statements on production is not advisable. Its counter productive and it might throw an exception if the browser does not support it. – Aditya Commented Jan 28, 2015 at 10:58
  • 2 generally leaving anything in production that isn't required is a bad idea. There's not harm in leaving console.log() in your production code, but it increases file size. Also you shouldn't need to debug in production code – martpendle Commented Jan 28, 2015 at 10:58
  • possible duplicate of Is it a bad idea to leave firebug "console.log" calls in your producton JavaScript code? – JJJ Commented Jan 28, 2015 at 11:01
Add a ment  | 

4 Answers 4

Reset to default 3

Since these are not persistent logs you won't get much benefit out of them. Also it runs on every individuals machine since everyone has their own copy of the program. If you really need it, it would be better to have a variable that can toggle this feature. Espcially if you are targetting to debug a whole lot of pre-determined stuffs.

Client Side issues needs to be debugged slightly different from Server Side. Everyone has their own copy of the program. Browser-JS is run on client side you open the browser and all code that you wrote is with you in a full blown repl and debugging is easy pared to Server side where most likely you wouldn't have access to that system. It is so flexible that you could just override it when you are checking something in production right from your own browser without affecting anyone. Just override it with those console statements and debug the issue.

Its a good idea to have logs in Server side programming. It gives a lot of useful information and is persistent.

As said above, debugging code should not be present on a production environment.

However, if you plan to keep it anyway, keep in mind that all browsers do not support it.

So you may check its availability and provide a fallback:

if (typeof window.console === 'undefined')
{
    window.console = {};
}
if (typeof window.console.log === 'undefined')
{
    window.console.log = function() { };
}

According to mozilla developer network

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large inpatibilities between implementations and the behavior may change in the future.

Leaving console.log() on production sites can cause issues since it is not supported by older browsers. It is most likely to throw exceptions in that case.

It's historically considered a bad-practice by Javascript programmers. At the start of the eons, some browsers didn't have support for it (IE8 or less for example). Other thing to take in mind is that you are making your code larger to download.

本文标签: Should I keep console statements in JavaScript applicationStack Overflow