title: Log stdout + stderr and stderr simultaneously in R
body: <p>I want to have all <code>stdout + stderr</code> in one log file and simultaneously just <code>stderr</code> in another. How can I achieve this in R? Messages must keep the chronological order of all messages in both logs, for example say I have this code:</p>
  
  <pre><code>options(warn = 1)
  cat("1st stdout\n")
  warning("2nd stderr")
  cat("3rd stdout\n")
  warning("4th stderr")
  </code></pre>
  
  <p>How to make it redirect to both logs in the desired way?</p>
  
  <p>My thought that it could be simple if it was possible to merge and split the <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html" rel="nofollow noreferrer">R connections</a>, see the question here: <a href="https://stackoverflow.com/q/60925391/684229">How to merge and split R connections?</a></p>
  
  <p>I am not against special logging frameworks, but it is preferable to be able to log from functions like <code>cat</code>, <code>warning</code>, <code>message</code>, <code>stop</code> etc. without changing the code to different functions (since I also use different packages).</p>