I have a load of console.logs in my code, but I only need to see them/ turn them on if I need to see them on a site, so I want a way to be able to enable/ disbale them from in browser console by setting up a couple of snippets, such as: adysis.debug('enable') , adysis.debug('disable') but how can i get these to them trigger the console.logs in the code, I got no clue at all at the moment on how to do it?
Asked
Active
Viewed 83 times
1
-
1Does this answer your question? [How to quickly and conveniently disable all console.log statements in my code?](https://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code) – GrafiCode Aug 29 '23 at 10:37
1 Answers
2
To disable ALL the console's methods use Proxy
.
Use console.enabled = true/false
in the browser console to disable/enable console.
The disabled state is persisted in the local storage. So it's the same after a page reload.
(Remove try/catch
since it's not allowed here).
{
const key = 'console-enabled';
let enabled;
try{
enabled = localStorage.getItem(key);
}catch(_){}
window.console = new Proxy(window.console, {
get(console, prop){
if(prop === 'enabled'){
return disabled;
}
if(!enabled && typeof console[prop] === 'function' && Object.hasOwn(console, prop)){
return function(){}
}
return Reflect.get(...arguments);
},
set(console, prop, val){
if(prop === 'enabled'){
enabled = val;
try{
enabled ? localStorage.setItem(key, val) : localStorage.removeItem(key);
}catch(_){}
return true;
}
return Reflect.set(...arguments);
}
});
}
console.log('disabled');
console.dir(document.body);
$pre.textContent = 'console.log is enumerable: ' + console.propertyIsEnumerable('log');
console.enabled = true;
console.log('enabled');
console.dir(document.body);
<pre id="$pre"></pre>

Alexander Nenashev
- 8,775
- 2
- 6
- 17
-
how do i turn it on/ off in browser console? i need a "dummy" method/ drop in, i aint a good coder lol – David Stanton Aug 29 '23 at 10:58
-
@DavidStanton fixed, you can use `console.disabled = true/false` – Alexander Nenashev Aug 29 '23 at 11:04
-
this leaves logs on to start, i want off to start, so can turn on? – David Stanton Aug 29 '23 at 11:16
-
@DavidStanton fixed, put `console.disabled = true` right in the beginning or use the second option where the console is disabled at the start – Alexander Nenashev Aug 29 '23 at 11:20
-
ok, i see now logs OFF to start, but if I have a "snippet" that has: console.enabled = true; and "run" it, the logs dont appear, not evn on refresh? – David Stanton Aug 29 '23 at 11:24
-
@DavidStanton in the second snippet `console.enabled = true` works fine – Alexander Nenashev Aug 29 '23 at 11:30
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/255098/discussion-between-david-stanton-and-alexander-nenashev). – David Stanton Aug 29 '23 at 11:31
-
it only works "in code" not from in browser console, which deafeat the object of what i'm trying to do, which is be able to turn it on/ of from in browser console? – David Stanton Aug 29 '23 at 11:42
-
ah hang on, it does "sort of" work, but it only shows the logs that take place after turned on, not "all" the logs on turn on and doesnt work on "refresh" so as to try and get "all" logs – David Stanton Aug 29 '23 at 11:48
-
@DavidStanton i see, you want to disable/enable permanently – Alexander Nenashev Aug 29 '23 at 12:01
-
-
@DavidStanton, done, you can disable/enable in the browser console – Alexander Nenashev Aug 29 '23 at 13:56
-
thnaks for the help here, much appreciated but this seems to have chnaged to the oposite again now, its "logs on" first page load, but should be "logs off". I'm trying to use this accross multiple websites, where my "code lives", so i want a "normal user" to not see the logs, but i have the abilty to go to the site turn on/ off, default/ start point is off – David Stanton Aug 29 '23 at 14:32
-
@DavidStanton done, disabled by default you should enable it in the console by typing `console.enabled = true` – Alexander Nenashev Aug 29 '23 at 14:46