Issue #18244 has been updated by inversion (Yura Babak).


> Why mixing `stdout` and `stderr`?

I have a script that is running periodically in the background on another machine. Sometimes I visit that machine and review logs. 
My goal is to see a full story in the log, the same as we can see when running in the terminal. 
For that I use the next running command:
`ruby task.rb >> log.txt  2>&1`
And I would like to see any crash exception as the **last thing** (as it is in the terminal), not in the middle of all the outputs regardless the buffering. So by outputs order, I can analyze the sequence of the last operations.
Maybe there is some other approach to achieve this?

This issue that the final exception ignores buffering, gave me hard times analyzing logs until I realized that it is impossible order of outputs and it is just the wrong output order to file.


----------------------------------------
Bug #18244: Unexpected errors output order
https://bugs.ruby-lang.org/issues/18244#change-94067

* Author: inversion (Yura Babak)
* Status: Feedback
* Priority: Normal
* ruby -v: Ruby v3.0.2.107 (2021-07-07) [x64-mingw32]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
Having this code:
```ruby
# $stderr.sync = false
warn 'warn'
$stderr.puts 'err'
fail
```
When I run it like this `ruby test.rb 2>err.log`, output is ok:
```
warn
err
test.rb:3:in `<main>': unhandled exception
```

But when I uncomment the first line ― the output is different:
```
warn
test.rb:3:in `<main>': unhandled exception
err
```
Why buffered output to the `$stderr` redirected to file has the **wrong order**?
If we do not redirect to the file ― the order is ok (exception is the last line of the output).

Checked platforms:
`Windows10: Ruby v3.0.2.107 (2021-07-07) [x64-mingw32]`
`Linux: Ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]`



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>