Problem: Log Every Property Read on a Config Object

Easy
15 min
Intercept every property access and log which key was read.

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 Proxy and the get trap.

  • Do not modify the original config object.

  • 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: appName
NodeMaster
*/
console.log(monitoredConfig.debug);
/* Expected output:
Accessed: debug
true
*/
console.log(monitoredConfig.version);
/* Expected output:
Accessed: version
1.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

Easy
15 min
Intercept every property access and log which key was read.

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 Proxy and the get trap.

  • Do not modify the original config object.

  • 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: appName
NodeMaster
*/
console.log(monitoredConfig.debug);
/* Expected output:
Accessed: debug
true
*/
console.log(monitoredConfig.version);
/* Expected output:
Accessed: version
1.2.0
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node,js
// Simulated configuration
const config = {
appName: 'NodeMaster',
version: '1.2.0',
debug: true
};
// Your code here
// Example usage
console.log(monitoredConfig.appName);
/* Expected output:
Accessed: appName
NodeMaster
*/
console.log(monitoredConfig.debug);
/* Expected output:
Accessed: debug
true
*/
console.log(monitoredConfig.version);
/* Expected output:
Accessed: version
1.2.0
*/