Problem: Log Every Property Read on a Config Object
Problem statement
Your team’s configuration object is large and used across dozens of modules. Recently, some properties have been accessed that no longer exist, leading to silent, undefined issues.
You need a quick way to observe which configuration keys are actually being read at runtime, without modifying existing code that consumes them. You’ll use the Proxy Pattern to intercept property access and log the property name whenever it’s read.
Goal
Wrap the given configuration object config with a proxy that logs each accessed property name and returns the corresponding value.
Constraints
You must use a
Proxyand thegettrap.Do not modify the original
configobject.Do not hardcode property names or wrap each key manually.
Return the actual value after logging.
Sample output
The examples below illustrate what the output should look like:
console.log(monitoredConfig.appName);=/* Expected output:Accessed: appNameNodeMaster*/console.log(monitoredConfig.debug);/* Expected output:Accessed: debugtrue*/console.log(monitoredConfig.version);/* Expected output:Accessed: version1.2.0*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Log Every Property Read on a Config Object
Problem statement
Your team’s configuration object is large and used across dozens of modules. Recently, some properties have been accessed that no longer exist, leading to silent, undefined issues.
You need a quick way to observe which configuration keys are actually being read at runtime, without modifying existing code that consumes them. You’ll use the Proxy Pattern to intercept property access and log the property name whenever it’s read.
Goal
Wrap the given configuration object config with a proxy that logs each accessed property name and returns the corresponding value.
Constraints
You must use a
Proxyand thegettrap.Do not modify the original
configobject.Do not hardcode property names or wrap each key manually.
Return the actual value after logging.
Sample output
The examples below illustrate what the output should look like:
console.log(monitoredConfig.appName);=/* Expected output:Accessed: appNameNodeMaster*/console.log(monitoredConfig.debug);/* Expected output:Accessed: debugtrue*/console.log(monitoredConfig.version);/* Expected output:Accessed: version1.2.0*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.