Problem: Implement a Chainable Logger Config Builder

Medium
30 min
Construct a logger config object step-by-step using a chainable interface that supports multiple output targets.

Problem statement

You’re building a custom logger for an internal tool. Developers should be able to define logger behavior fluently, including log level, output format, and the destination of logs (e.g., console, file, or remote). Instead of passing around nested config objects, you want a builder that guides developers through constructing a valid logger config step-by-step.

Goal

Implement a LoggerConfigBuilder class with the following chainable methods:

  • .setLevel(level: string): Sets the log level (e.g., "debug", "warn").

  • .useJsonFormat(): Enables JSON output format.

  • .toConsole(): Enables logging to the console.

  • .toFile(path: string): Sets the file path for log output.

  • .toRemote(endpoint: string): Sets a remote endpoint for log forwarding.

  • .build(): Returns the final logger configuration object with shape.

{
level?: string,
format?: 'json',
outputs?: {
console?: true,
file?: string,
remote?: string
}
}

At least one output (console, file, or remote) must be configured. If none is set, .build() should throw.

Constraints

  • Only include keys that were explicitly configured.

  • Do not return undefined values in the final config.

  • Outputs must be grouped under the outputs key.

Sample output

The examples below illustrate what the output should look like:

const config1 = new LoggerConfigBuilder()
.setLevel('debug')
.useJsonFormat()
.toConsole()
.build();
console.log(config1);
/* Expected output:
{ level: 'debug', format: 'json', outputs: { console: true } } */
const config2 = new LoggerConfigBuilder()
.toFile('/var/log/app.log')
.toRemote('https://log.myapp.dev')
.build();
console.log(config2);
/* Expected output:
{ outputs:
{ file: '/var/log/app.log', remote: 'https://log.myapp.dev' } } */
const config3 = new LoggerConfigBuilder()
.setLevel('warn')
.build(); // should throw
/* Expected output:
Failed to build config3: At least one output must be configured */

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

Problem: Implement a Chainable Logger Config Builder

Medium
30 min
Construct a logger config object step-by-step using a chainable interface that supports multiple output targets.

Problem statement

You’re building a custom logger for an internal tool. Developers should be able to define logger behavior fluently, including log level, output format, and the destination of logs (e.g., console, file, or remote). Instead of passing around nested config objects, you want a builder that guides developers through constructing a valid logger config step-by-step.

Goal

Implement a LoggerConfigBuilder class with the following chainable methods:

  • .setLevel(level: string): Sets the log level (e.g., "debug", "warn").

  • .useJsonFormat(): Enables JSON output format.

  • .toConsole(): Enables logging to the console.

  • .toFile(path: string): Sets the file path for log output.

  • .toRemote(endpoint: string): Sets a remote endpoint for log forwarding.

  • .build(): Returns the final logger configuration object with shape.

{
level?: string,
format?: 'json',
outputs?: {
console?: true,
file?: string,
remote?: string
}
}

At least one output (console, file, or remote) must be configured. If none is set, .build() should throw.

Constraints

  • Only include keys that were explicitly configured.

  • Do not return undefined values in the final config.

  • Outputs must be grouped under the outputs key.

Sample output

The examples below illustrate what the output should look like:

const config1 = new LoggerConfigBuilder()
.setLevel('debug')
.useJsonFormat()
.toConsole()
.build();
console.log(config1);
/* Expected output:
{ level: 'debug', format: 'json', outputs: { console: true } } */
const config2 = new LoggerConfigBuilder()
.toFile('/var/log/app.log')
.toRemote('https://log.myapp.dev')
.build();
console.log(config2);
/* Expected output:
{ outputs:
{ file: '/var/log/app.log', remote: 'https://log.myapp.dev' } } */
const config3 = new LoggerConfigBuilder()
.setLevel('warn')
.build(); // should throw
/* Expected output:
Failed to build config3: At least one output must be configured */

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

Node.js
class LoggerConfigBuilder {
// implement methods here
}
// Example usage:
const config1 = new LoggerConfigBuilder()
.setLevel('debug')
.useJsonFormat()
.toConsole()
.build();
console.log(config1);
/* Expected output:
{ level: 'debug', format: 'json', outputs: { console: true } } */
const config2 = new LoggerConfigBuilder()
.toFile('/var/log/app.log')
.toRemote('https://log.myapp.dev')
.build();
console.log(config2);
/* Expected output:
{ outputs:
{ file: '/var/log/app.log', remote: 'https://log.myapp.dev' } } */
const config3 = new LoggerConfigBuilder()
.setLevel('warn')
.build(); // should throw
/* Expected output:
Failed to build config3: At least one output must be configured */