Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be removing console.log from production code?

I currently have this JS statement everywhere in my code:

window.console && console.log("Foo"); 

I am wondering if this is costly at all, or has any negative side-effects in production.

Am I free to leave client-side logging in, or should it go?

EDIT: In the end, I suppose the best argument I (and anyone else?) can come up with is that there is a possibly non-negligible amount of extra data transferred between the server and the client by leaving logging messages left in. If production code is to be fully optimized, logging will have to be removed to reduce the size of javascript being sent to the client.

like image 678
Sean Anderson Avatar asked Nov 03 '11 21:11

Sean Anderson


People also ask

Can I use console log in production?

You can use your console just like a normal server side logging platform. During production, if you're worried that your users might see things, you can turn off console visibility in production with the following configuration: atatus.

Does console log affect performance?

console. log by itself doesn't really impact performance in a way that you'll notice unless you bind it to a scroll / resize handler. These get called alot and if your browser has to send text to the console like 30/60x a second it can get ugly.

Can you delete console log?

Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console.

What does console log do in coding?

console. log specifically is a method for developers to write code to inconspicuously inform the developers what the code is doing. It can be used to alert you that there's an issue, but shouldn't take the place of an interactive debugger when it comes time to debug the code.


2 Answers

Another way of dealing with this is to 'stub' out the console object when it isn't defined so no errors are thrown in contexts that do not have the console i.e.

if (!window.console) {   var noOp = function(){}; // no-op function   console = {     log: noOp,     warn: noOp,     error: noOp   } } 

you get the idea... there are a lot of functions defined on the various implementations of the console, so you could stub them all or just the ones you use (e.g. if you only ever use console.log and never used console.profile, console.time etc...)

This for me is a better alternative in development than adding conditionals in front of every call, or not using them.

see also: Is it a bad idea to leave "console.log()" calls in your producton JavaScript code?

like image 192
craigb Avatar answered Oct 17 '22 01:10

craigb


You should not add development tools to a production page.

To answer the other question: The code cannot have a negative side-effect:

  • window.console will evaluate to false if console is not defined
  • console.log("Foo") will print the message to the console when it's defined (provided that the page does not overwrite console.log by a non-function).
like image 22
Rob W Avatar answered Oct 16 '22 23:10

Rob W