Share

Capturing colorized terminal output while preserving both colored display and clean log files requires specific command-line techniques. This guide provides a tested method to maintain terminal colors while creating plain-text log files, addressing a common challenge for developers working with tools like Node.js servers. The solution centers on using Unix pipes and GNU sed to split the output stream effectively.
Many command-line tools, like those using the chalk library for Node.js, automatically detect if their output is a terminal (TTY). When output is directed to a file or a pipe, these tools often strip color codes to avoid polluting log files with ANSI escape sequences. While this is useful for clean logs, it means simple commands like your-command > logfile.txt result in a loss of color in the terminal. Conversely, using a tool like script to capture output preserves colors on-screen but saves those raw escape codes into the log file, making it difficult to search and read.
The most effective method uses a combination of Unix utilities to trick the application into thinking it's always outputting to a terminal. This ensures color codes are generated, after which they can be filtered into a separate stream. The core command structure involves the unbuffer command (part of the expect package) and GNU sed.
Step-by-Step Implementation:
Install Required Dependencies: On macOS using Homebrew, you would install the necessary packages:
brew install expect gnu-sed
The expect package provides unbuffer, a pseudo-terminal wrapper. The gnu-sed package offers more advanced scripting capabilities than the default macOS sed.
Construct the Command: The following command structure splits the output, sending stripped, clean text to a file and colored output to the terminal.
unbuffer your-command-here 2>&1 | gsed -u -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" | tee clean-output.log
unbuffer: Forces the command to behave as if it is running in a TTY, ensuring color codes are generated.2>&1: Redirects standard error (stderr) to standard output (stdout) to capture all messages.gsed -u -r "s/...//g": The GNU sed command uses a regular expression to find and remove ANSI escape codes. The -u flag ensures unbuffered output for real-time logging.tee clean-output.log: Sends the cleaned output both to the terminal and to the specified log file.Based on our experience assessment, this method is highly effective but has two key considerations. First, Unix pipes may not always flush on an interrupt signal (SIGINT), such as stopping a server with Cmd+C. This means the last few lines of unbuffered logs might not make it to the file. Second, this technique is not limited to specific applications; it works for any command with colorized output, such as ls --color=auto.
To successfully capture both a colorized terminal view and a searchable log file, use the unbuffer command with GNU sed to filter out ANSI color codes. This approach resolves the core conflict between human-readable colors and machine-readable log files, streamlining the debugging process for development servers and other command-line tools.









