or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-6/

CLI Logger with Configurable Color Output

Problem Statement

Build a command-line logging utility that can output formatted log messages with different severity levels. The logger must support configuration-based color control, allowing users to enable or disable color output via a configuration file, independent of terminal capabilities.

Requirements

Your implementation must support:

  1. Multiple Log Levels: The logger should support at least three severity levels:

    • info: Informational messages
    • warn: Warning messages
    • error: Error messages
  2. Configuration-Based Color Control: The logger must read a configuration file (config.json) that contains a boolean enableColors field. This configuration should control whether log output includes color formatting, regardless of terminal capabilities.

  3. Color Formatting: When colors are enabled:

    • Info messages should use cyan text
    • Warning messages should use yellow text
    • Error messages should use red text
  4. Consistent API: The logger should work the same way whether colors are enabled or disabled - only the output formatting should differ.

Implementation Files

Create the following files:

logger.js

The main logger module that:

  • Exports a createLogger(config) function that returns a logger instance
  • The returned logger should have methods: info(message), warn(message), and error(message)
  • Each method should output the message with appropriate formatting based on the configuration

config.json

A JSON configuration file with the structure:

{
  "enableColors": true
}

logger.test.js { .test }

Test file containing the following test cases:

Test 1: @test When enableColors is true, calling logger.info("test message") should output text that includes ANSI color escape codes (specifically the cyan color code \x1b[36m).

Test 2: @test When enableColors is false, calling logger.info("test message") should output plain text without any ANSI escape codes (no \x1b[ sequences).

Test 3: @test When enableColors is true, the logger should format different severity levels with different colors:

  • logger.warn("warning") should include yellow color codes (\x1b[33m)
  • logger.error("error") should include red color codes (\x1b[31m)

Dependencies { .dependencies }

picocolors { .dependency }

Provides terminal color formatting support.

Example Usage

const createLogger = require('./logger');
const config = require('./config.json');

const logger = createLogger(config);

logger.info('Application started');
logger.warn('Configuration file is outdated');
logger.error('Failed to connect to database');

Notes

  • Your implementation should not check terminal capabilities automatically - it must respect the configuration setting
  • The same logger instance should consistently apply the color preference to all log levels
  • Messages should be output to stdout or console