At Redfin, we’ve written a wrapper script around our React server to make it easier to use for developers who aren’t familiar with Node.js that we call Corvair (we’re apparently super into muscle cars or something — our api server is called Stingray). When you run `corvair`, you get a bunch of really pretty logs that are colorized with helpful information that looks like this
The colorization comes from gulp, which uses one of personal heroes, Sindre Sordhus’s fantastic little library, chalk. This is great, except that your terminal emulator likely has a maximum amount history that it saves, which can make it frustrating to debug complex issues, since you either have to scroll extensively, or you may even lose your logs altogether. There is a great little application called `tee` that allows you to split a stream in two, outputting it simultaneously to the console and to a file:
corvair -bs | tee corvair.log
But `chalk` detects if its output is a tty, and strips the color codes if it is not:
You can get around this problem by running `corvair` using `script`, which executes a command in a terminal, and allows you to output the results to a file
script -q corvair.log corvair -bs
And successfully preserves colors in the terminal window
However, this means that `corvair.log` now has all the terminal color escape codes in it
Gross — now all those magical regexes I get out of our internal wikis use to `grep` for some build event or error message don’t work because there’s weird garbage in the log file. Luckily, this problem is solveable with some unixy goodness. First we’ll need to install some dependencies (we use OS X for our dev machines, so we use the indispensable homebrew to manage packages)
brew install homebrew/dupes/expect brew install coreutils brew install gnu-sed
And add the new unix `coreutils` to your path by updating your `.zshrc` or `.bashrc`
export PATH="$(brew --prefix coreutils)/libexec/gnubin:/usr/local/bin:$PATH"
And now we can successfully trick `corvair` into thinking its output is to a tty even when used in a pipe
unbuffer corvair -bs | tee corvair.out
And use gnu sed to output the terminal color escapes to stderr, and the rest of the output to stdout, and get a log file with no color escapes while preserving colors in the terminal output.
unbuffer corvair -bs | gsed -r 'w /dev/stderr' | gsed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' > corvair.out
One word of caution — unix pipes do not necessarily flush on SIGINT, so you may lose some unbuffered logs at the end of your file if you take your server down with a Cmd+C. And, of course, this works for any colorized output, like `ls –color=auto`.