Is there any way to turn off all console.log
statements in my JavaScript code, for testing purposes?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I know you asked how to disable console.log, but this might be what you're really after. This way you don't have to explicitly enable or disable the console. It simply prevents those pesky console errors for people who don't have it open or installed.
If you're using IE7, console won't be defined. So a more IE friendly version would be:
This should override all methods of window.console. You can put it on the very top of your scripts section, and if you are on a PHP framework you can only print this code when your app environment is production, or if some kind of debug flag is disabled. Then you would have all your logs in your code working on development environments or in debug mode.
If you use Grunt you can add a task in order to remove/comment the console.log statements. Therefore the console.log are no longer called.
https://www.npmjs.org/package/grunt-remove-logging-calls
You could use javascript AOP (e.g. jquery-aop) to intercept all calls to console.debug/log (around) and do not proceed with the actual invocation if some global variable is set to false.
You could even do an ajax call (now and then) so you can change the log enabled/disabled behavior on the server which can be very interesting to enable debugging when facing an issue in a staging environment or such.
After I searched for this issue aswell and tried it within my cordova app, I just want to warn every developer for windows phone to not overwrite
because the app will crash on startup.
It won't crash if you're developing local if you're lucky, but submitting in store it will result in crashing the app.
Just overwrite
if you need to.
This works in my app: