I have many console.log
(or any other console calls) in my code and I would like to use them only
when my app is in some kind of "debug mode".
I can't seem to use some kind of logger function and internally use console.log
because then I wouldn't know what line fired it. Maybe only with a try/catch, but my logs are very general and I don't want try/catch in my code.
What would you recommend?
Clobbering global functions is generally a bad idea.
Instead, you could replace all instances of
console.log
in your code withLOG
, and at the beginning of your code:This will still show correct line numbers and also preserve the expected
console.log
function for third party stuff if needed.Nowdays, in 2014, I simply use GULP (and recommend everyone to, it's an amazing tool), and I have a package installed which is called stripDebug which does that for you.
(I also use
uglify
andclosureCompiler
in production)console can out put not just log but errors warnings etc. Here is a function to override all console outputs
Refer to detailed post here https://stapp.space/disable-javascript-console-on-production/
Simple.
Add a little bash script that finds all references to
console.log
and deletes them.Make sure that this batch script runs as part of your deployment to production.
Don't shim out
console.log
as an empty function, that's a waste of computation and space.The newest versions of chrome show which line of code in which file fired console.log. If you are looking for a log management system, you can try out logeek it allows you to control which groups of logs you want to see.