Skip to content

[FEATURE] Support standard color control environment variables. Use colored logs by default in docker contexts. #2149

@markope

Description

@markope

Feature Description

Summary: Support NO_COLOR/FORCE_COLOR environment variables and improve color detection for Docker environments

Problem Statement:
When running crossbar in Docker containers or non-TTY environments, log output appears as plain uncolored text because the current color detection relies solely on sys.__stdout__.isatty(). This makes logs harder to read and doesn't follow modern CLI conventions for color control. Users have no way to force colors on or off via environment variables.

Proposed Solution:
Enhance the color auto-detection logic in _start_logging() to:

  1. Respect NO_COLOR environment variable to explicitly disable colors (per https://no-color.org/)
  2. Respect FORCE_COLOR and CLICOLOR_FORCE environment variables to explicitly enable colors
  3. Default to enabling colors when in auto mode

Use Cases

Use Case 1:

  • Actor: DevOps engineer running crossbar in Docker
  • Goal: View colored log output via docker logs for easier debugging
  • Example: docker logs crossbar-container displays colored output distinguishing log levels

Use Case 2:

  • Actor: Developer with accessibility needs or specific terminal
  • Goal: Disable colors that may not render correctly
  • Example: NO_COLOR=1 crossbar start runs without any ANSI color codes

Use Case 3:

  • Actor: CI/CD pipeline operator
  • Goal: Force colors in CI logs that support ANSI (GitHub Actions, GitLab CI)
  • Example: FORCE_COLOR=1 crossbar start ensures colored output in CI logs

Example API/Usage

# Force colors on (Docker, CI/CD)
FORCE_COLOR=1 crossbar start

# Force colors off (accessibility, piping to file)
NO_COLOR=1 crossbar start

# Auto-detect (default) - now defaults to colors enabled
crossbar start
# Current behavior in _start_logging():
if color == "auto":
    if sys.__stdout__.isatty():
        color = True
    else:
        color = False  # Always disabled in Docker

# Proposed behavior:
if color == "auto":
    if os.environ.get('NO_COLOR'):
        color = False
    elif os.environ.get('FORCE_COLOR') or os.environ.get('CLICOLOR_FORCE'):
        color = True
    elif sys.__stdout__.isatty():
        color = True
    else:
        color = True  # Default to colors enabled

Alternatives Considered

Alternative 1: Only add TTY detection improvement

  • Pros: Minimal change
  • Cons: No user control via environment variables

Alternative 2: Add a new CLI flag --force-color

  • Pros: Explicit control
  • Cons: Doesn't follow industry standards, requires config changes

Why the proposed solution is better: Uses widely-adopted conventions (NO_COLOR, FORCE_COLOR, CLICOLOR_FORCE) that users already know from other tools. No new CLI flags needed, works with existing --color=auto option.

Impact Assessment

Breaking Changes: Minor - logs that previously had no color in non-TTY environments will now show colors by default. Users can set NO_COLOR=1 to restore previous behavior.

Affected Components:

  • Core library (src/crossbar/node/main.py)
  • API
  • Documentation
  • Tests
  • CI/CD

Estimated Complexity: Low

Additional Context

Related Issues:

  • None known

External References:

Checklist

  • I have searched existing issues to avoid duplicates
  • I have described the problem clearly
  • I have provided use cases
  • I have considered alternatives
  • I have assessed impact and breaking changes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions