We basically use the console.log() in our JS application to check wether our code is working properly or to replicate the bug or issue in the app. ...
For further actions, you may consider blocking this person and/or reporting abuse
There is plugin available to remove a
console.log
statements from project source called babel-plugin-transform-remove-consoleUsage:
add plugin name in
.babelrc
file.This is a better option than assigning an empty function to
console.log
. The empty function removes the option to log anything in your production environment, even for debugging purposes.If you are using Terser in your build, they provide an option to remove log statements (drop_console: true) from your minified code
How do I achieve this in the modern cra-generated applications, where
.babelrc
file is not exposed and requires me toeject
from the react app?Thanks for your suggestions, I will try this one also
sure ðð»
That is one way to do it, and it certainly is simple. However, you're still shipping the code that makes those calls in the first place.
To address that, one could take advantage of the bundling process to strip out all those calls in the first place. For example, it looks like UglifyJS supports conditional compiling that will help with that.
Thanks! - A+ for the
undercover
dog.Off-topic: The pictures are so CUTE!
As said in comments, I'd go with transformers in build time. But in case one is going with the article's approach I'd suggest something like:
I wrap my console.log function in my own function.
Hopefully this helps someone.
Something like:
function myLog(level, input) {
if(!AppDebug && level < AppDebugThreshold) return;
console.log({level:level, log:input});
}
So in your overall JavaScript window / class / enclosure just set the variable
AppDebug
totrue
to log everything, or leave it false and setAppDebugThreshold
to ainteger
to only log debug messages above that "level."Use turbo log an extension in VSCode that'll remove all the console.logs you were working on. I see no reason to get more lost in your code than that
I've recently created a small library that let you hide all your console statements. I also added some configuration to show the statements for debugging adding a custom key to the local storage. And you can set some initial styled messages to be shown on the console of your site. Just check it out:
github.com/eaboy/clean-console
Any feedback is very welcome.
Off topic: I read somewhere that there are so many other functions in the console object. Any way to customize their outputs for Node in terminal?
I know the different outputs look different in the browser, though. Just wondering if that'd be possible in Node
I have a strict eslint + husky policy for console logs on the projects I run because I always forget about them ð
would you like to share it? :)
We can also use the Flag devMode to disable console log in production full article :- stacksjar.com/post/disable-console...
Thanks for that! Many times I found an log message on my console on production ð ð
wow .. saved me ages.
In Angular You get the Built in Feature to Detect the application environment so you can Disable console log in production check this: stacksjar.com/post/disable-console...