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 wrote this:
Unfortunately it doesn't work on use strict mode.
So using
console.fn.setEnabled = setEnabled
and thenconsole.fn.setEnabled(false)
wherefn
could be almost any console function. For your case would be:I wrote this too:
so then you just need to put in the
FLAGS
the default value (before execute the above code), likeFLAGS.LOG = false
and the log function would be disabled by default, and still you could enabled it callingconsole.log.setEnabled(true)
Warning: Shameless plug!
You could also use something like my JsTrace object to have modular tracing with module-level "switching" capability to only turn on what you want to see at the time.
http://jstrace.codeplex.com
(Also has a NuGet package, for those who care)
All levels default to "error", though you can shut them "off". Though, I can't think of why you would NOT want to see errors
You can change them like this:
Fore more docs, check out the Documentation
T
Ive been using the following to deal with he problem:-
Set debug to 1 to enable debugging. Then use the logger function when outputting debug text. It's also set up to accept two parameters.
So, instead of
use
Just change the flag
DEBUG
to override the console.log function. This should do the trick.My comprehensive solution to disable/override all
console.*
functions is here.Of course, please make sure you are including it after checking necessary context. For example, only including in production release, it's not bombing any other crucial components etc.
Quoting it here:
The following is more thorough:
This will zero out the common methods in the console if it exists, and they can be called without error and virtually no performance overhead. In the case of a browser like IE6 with no console, the dummy methods will be created to prevent errors. Of course there are many more functions in Firebug, like trace, profile, time, etc. They can be added to the list if you use them in your code.
You can also check if the debugger has those special methods or not (ie, IE) and zero out the ones it does not support: