'JavaScirpt - window.console proxy
I'm trying to persist console.log, console.warn...
Is it possible to have a proxy on window.console?
I tried:
new Proxy(window.console, {
get(target, key) {
const p = target[key];
debugger; // Never gets here!
if (['error', 'info', 'log', 'warn'].includes(key)) {
return async function(...args) {
p.apply(target, args);
// Persist log
debugger;
}
}
return p;
},
});
Solution 1:[1]
You have to assign to the object you want to modify.
window.console = new Proxy(window.console, {
get(target, key) {
const p = target[key];
debugger; // Never gets here!
if (['error', 'info', 'log', 'warn'].includes(key)) {
return async function(...args) {
p.apply(target, args);
// Persist log
debugger;
}
}
return p;
},
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LudvĂk Prokopec |
