title: Bash: log stdout and stderr while preserving order and provenance
body: <p>It's fairly simple to log both the stdout and the stderr of a command to a log file:</p>
  
  <pre><code>./foo.sh &amp;&gt; log.txt
  </code></pre>
  
  <p>The problem is that when inspecting the log file, one doesn't know anymore which line was coming from which stream. This could be fixed by redirecting stdout and stderr to two separate files, but then the chronology and interleaving of the output is lost.</p>
  
  <p>An other solution would be to redirect to three files. One with the stdout, one with the stderr, and one with both combined. Something like:</p>
  
  <pre><code>./foo.sh 2&gt; &gt;(tee stderr | tee -a combined) 1&gt; &gt;(tee stdout | tee -a combined)
  </code></pre>
  
  <p>But that is not be very elegant to have so many files (and this command still dumps a copy of the output on the shell).</p>
  
  <p>I found an interesting bash function that would color only stderr messages in red:</p>
  
  <pre><code>color()(set -o pipefail;"$@" 2&gt;&amp;1&gt;&amp;3|sed $'s,.*,\e[31m&amp;\e[m,'&gt;&amp;2)3&gt;&amp;1
  </code></pre>
  
  <p>but it doesn't preserve the order of the output and the result is unreadable in a text editor. Given the following program for <code>foo.sh</code>:</p>
  
  <pre><code>for i in 1 2; do
     for j in 1 2; do
       printf '%s\n' "out $i"
     done
     for k in 1 2; do
       printf '%s\n' "err $i" &gt;&amp;2
     done
  done
  </code></pre>
  
  <p>Running <code>color ./foo.sh</code> produces:</p>
  
  <pre><code>out 1
  out 1
  out 2
  out 2
  [31merr 1[m
  [31merr 1[m
  [31merr 2[m
  [31merr 2[m
  </code></pre>
  
  <p>How could one easily end up with something such as this in a single log file ?</p>
  
  <pre><code>@| out 1
  @| out 1
  $| err 1
  $| err 1
  @| out 2
  @| out 2
  $| err 2
  $| err 2
  </code></pre>