Logging To Stdout And Stderr

Posted on the 15 May 2016 by Hean Hong Leong @hongster

There are many sophisticated logging tools that provide advanced features like analytics. Depending on your application needs, sometimes all we want is just simple logging. For such, I recommend logging to standard output and standard error.

1 of the advantages is flexibility of redirecting/piping of log data to files and applications. It also works on most programming languages, including Go. I will be Go for illustration.

I wrote a simple logger (https://github.com/hongster/goldilocks), that logs output to standard streams by default. Informational and warning messages to Stdout, error messages to Stderr. Using the example application in README,

I can redirect standard and error log to separate files

./myapp > log.txt 2> error.txt

Combine all log into single file

./myapp &> combined.txt

Capture only non-error messages

./myapp > log.txt

Pass the log into another application for processing

./myapp 2>&1 | gzip > log.txt.gz

This is quite common among Linux applications.