Skip to content

[BUG] Terminal colors encodings visible in app logs #1366

Open

Description

@cichyx96

Terminal colors should be disabled/filtered out when presented in web app.

Image

Activity

  1. cray44 commented on May 8, 2026

    @cray44

    Hi I'd like to take a look at this. It looks like subprocess output containing ANSI escape sequences is being passed through to the web app logs without stripping. I'm planning to add an ANSI-sanitizing utility and apply it at the Terraform/Ansible output capture points. Will submit a PR once I have a working fix.

  2. janderik commented on Jul 22, 2026

    @janderik

    This is a common issue when terminal output with ANSI color codes is displayed in a web interface. The color codes (like \x1b[31m for red) are not being stripped before rendering.

    Solutions:

    1. Strip ANSI codes before logging:
    import re
    
    def strip_ansi_codes(text: str) -> str:
        """Remove ANSI escape codes from text."""
        ansi_escape = re.compile(r'\x1b\[[0-9;]*m')
        return ansi_escape.sub('', text)
    
    # Use when logging
    logger.info(strip_ansi_codes(output))
    1. Use a logging filter:
    import logging
    import re
    
    class ANSI stripFilter(logging.Filter):
        def __init__(self):
            super().__init__()
            self.ansi_escape = re.compile(r'\x1b\[[0-9;]*m')
        
        def filter(self, record):
            record.msg = self.ansi_escape.sub('', record.msg)
            return True
    
    # Add to logger
    logger.addFilter(ANSI stripFilter())
    1. Disable colors in non-TTY environments:
    import sys
    import os
    
    def should_use_colors():
        """Check if we should use colors (TTY, not piped, etc.)."""
        return (
            hasattr(sys.stdout, 'isatty') and 
            sys.stdout.isatty() and 
            os.environ.get('TERM') != 'dumb'
        )
    
    # Disable colors when not in a terminal
    use_colors = should_use_colors()
    1. For subprocess output:
    import subprocess
    
    result = subprocess.run(
        command,
        capture_output=True,
        text=True,
        env={**os.environ, 'TERM': 'dumb'}  # Force no colors
    )

    The cleanest solution is usually #1 (strip at log time) or #3 (prevent colors in non-TTY environments).

    Happy to help review a PR implementing any of these approaches!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions