Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Created October 7, 2025 10:40
Show Gist options
  • Select an option

  • Save joshnuss/57618adb4a421433ba3979e33faa1dfb to your computer and use it in GitHub Desktop.

Select an option

Save joshnuss/57618adb4a421433ba3979e33faa1dfb to your computer and use it in GitHub Desktop.
Function logger using a Proxy
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