Skip to content

Instantly share code, notes, and snippets.

@ianwremmel
Last active May 24, 2019 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianwremmel/a983b42e76230ab5fa1e667d550835db to your computer and use it in GitHub Desktop.
Save ianwremmel/a983b42e76230ab5fa1e667d550835db to your computer and use it in GitHub Desktop.
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