Problem: Add Feature-Flagged Logger Instantiation

Easy
15 min
Create a logger factory that selects transports based on feature flags.

Problem statement

You’re rolling out a new remote logging service. In the meantime, some environments still use the existing file logger. Feature flags control which version is enabled. You need to centralize this logic in a factory, so the rest of the system can just call createLogger(flags). The flags object may include { useRemote: true } or { useRemote: false }.

Goal

Implement createLogger(flags) to return a new instance of either FileLogger or RemoteLogger, based solely on the useRemote flag.

Constraints

  • Do not use conditionals (if, switch, ternaries) to select which class to instantiate.

  • Do not instantiate loggers outside the factory.

  • The logger classes must be treated as interchangeable by consumers.

Sample output

The examples below illustrate what the output should look like:

const remoteLogger = createLogger({ useRemote: true });
console.log(remoteLogger.log('User signed in'));
/* Expected output:
Remote: User signed in */
const fileLogger = createLogger({ useRemote: false });
console.log(fileLogger.log('User updated profile'));
/* Expected output:
File: User updated profile */

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

Problem: Add Feature-Flagged Logger Instantiation

Easy
15 min
Create a logger factory that selects transports based on feature flags.

Problem statement

You’re rolling out a new remote logging service. In the meantime, some environments still use the existing file logger. Feature flags control which version is enabled. You need to centralize this logic in a factory, so the rest of the system can just call createLogger(flags). The flags object may include { useRemote: true } or { useRemote: false }.

Goal

Implement createLogger(flags) to return a new instance of either FileLogger or RemoteLogger, based solely on the useRemote flag.

Constraints

  • Do not use conditionals (if, switch, ternaries) to select which class to instantiate.

  • Do not instantiate loggers outside the factory.

  • The logger classes must be treated as interchangeable by consumers.

Sample output

The examples below illustrate what the output should look like:

const remoteLogger = createLogger({ useRemote: true });
console.log(remoteLogger.log('User signed in'));
/* Expected output:
Remote: User signed in */
const fileLogger = createLogger({ useRemote: false });
console.log(fileLogger.log('User updated profile'));
/* Expected output:
File: User updated profile */

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

Node.js
class FileLogger {
log(msg) {
return `File: ${msg}`;
}
}
class RemoteLogger {
log(msg) {
return `Remote: ${msg}`;
}
}
// Implement this
function createLogger(flags) {
// return appropriate logger instance
}
// Example usage
const remoteLogger = createLogger({ useRemote: true });
console.log(remoteLogger.log('User signed in'));
/* Expected output:
Remote: User signed in */
const fileLogger = createLogger({ useRemote: false });
console.log(fileLogger.log('User updated profile'));
/* Expected output:
File: User updated profile */