Preserving colorized server logs - Redfin Real Estate News

Preserving colorized server logs

by
Updated on October 5th, 2020

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

Screen Shot 2016-02-04 at 2.36.40 PM

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:

Screen Shot 2015-12-01 at 5.16.04 PM

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

Screen Shot 2015-12-01 at 4.45.53 PM

However, this means that `corvair.log` now has all the terminal color escape codes in it

Screen Shot 2015-12-01 at 4.47.14 PM

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
Screen Shot 2015-12-01 at 5.24.26 PM
Screen Shot 2015-12-01 at 5.26.09 PM

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`.

Avatar

Doug Wade

I'm a software developer at Redfin on the Platforms team. I'm a cyclist, soccer hooligan, and beer enthusiast.

Email Doug

Leave a Comment

Your email address will not be published. Required fields are marked *

Be the first to see the latest real estate news:

  • This field is for validation purposes and should be left unchanged.

By submitting your email you agree to Redfin’s Terms of Use and Privacy Policy

Scroll to Top