Output golang cmdline tools to stdout and file using tee


Golang · Stderr · Stdout · Tee

1 minute

Some golang-based tools I’ve used (or even written) use the builtin log package that output logs to stderr by default. I also use tee for piping console outputs to file for later viewing. This is the command I generally use:

# Example tool:
$ sometool --flag1 --flag2 2>&1 | tee out.txt

# 2>&1 <-- redirect stderr to stdout
# tee <-- pipe the console output to out.txt while retaining the actual console logs during command execution