<commit_before>---
layout: post
title: "Output golang cmdline tools to stdout and file using tee"
location: "Japan"
tags: [stern, grep, k8s, logs]
---

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 the [tee](https://en.wikipedia.org/wiki/Tee_(command)) for piping console outputs to file for later viewing. This is the command I generally use:

{% highlight shell %}
# Example tool:
$ sometool --flag1 --flag2 2>&1 | tee out.txt

# 2>&1 <-- redirect stderr to stdout<commit_msg>update tags<commit_after>---
layout: post
title: "Output golang cmdline tools to stdout and file using tee"
location: "Japan"
tags: [golang, tee, stderr, stdout]
---

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 the [tee](https://en.wikipedia.org/wiki/Tee_(command)) for piping console outputs to file for later viewing. This is the command I generally use:

{% highlight shell %}
# Example tool:
$ sometool --flag1 --flag2 2>&1 | tee out.txt

# 2>&1 <-- redirect stderr to stdout
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.011494252873563218, 'mean_word_length': 4.1454545454545455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3079358', 'n_tokens_mistral': 363, 'n_tokens_neox': 342, 'n_words': 154}