Created
October 7, 2025 10:40
-
-
Save joshnuss/57618adb4a421433ba3979e33faa1dfb to your computer and use it in GitHub Desktop.
Function logger using a Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function logger<T extends object>(target: T, methods: string[]): T { | |
| return new Proxy(target, { | |
| get(target, key) { | |
| const item = Reflect.get(target, key) | |
| if (!methods.includes(key.toString())) { | |
| return item | |
| } | |
| return (...args: any[]) => { | |
| console.log(`Called ${key.toString()}() with ${args}`) | |
| return item.call(target, ...args) | |
| } | |
| } | |
| }) | |
| } | |
| const object = { | |
| a: 1, | |
| b: 2, | |
| do_something(a: number, b: number, c: number) { | |
| console.log('hello world ' + [a,b,c]) | |
| } | |
| } | |
| const o = logger(object, ['do_something']) | |
| o.do_something(1,2,3) | |
| o.do_something(5,6,7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment