Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Last active May 24, 2019
Embed
What would you like to do?
Runtime-overridable logger
import {logger} from './lib/logger';
class X {
get lgr() {
return logger;
}
method() {
this.lgr.info('called');
}
}
const x = new X();
const y = new Proxy(x, {
get(target, prop, receiver) {
if (prop === 'lgr') {
const lgr = logger.child({proxied: true});
return lgr;
}
return target[prop];
},
});
y.method();
// prints
// {
// "proxied": true,
// "message": "called",
// "level": "info",
// "timestamp": "2019-05-24T00:43:36.881Z",
// "ms": "+0ms"
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment