Skip to content

Redirecting STDERR not working as expected #1082

Closed
MilesRoberts opened this Issue Oct 4, 2013 · 2 comments

2 participants

@MilesRoberts

Trying to redirect STDERR to log, $stderr.puts works, as does warn, but raised errors never make it to the log.

log = File.new('/home/my_user/test_log.log', "w")
$stderr.reopen log
$stderr.puts "writing to log!"
warn "warning!"

require 'some_broken_thing'

log contents in MRI (2.0.0-p0):

/path/to/ruby/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- some_broken_thing (LoadError)
  from /path/to/ruby/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
  from test.rb:5:in `<main>'
writing to log!
warning!

log contents in jruby (1.7.4):

writing to log!
warning!

When I call $stderr.reopen, the error output from the broken require doesn't show up in the console, which is good, but it also never shows up in the file, which isn't so good.

@MilesRoberts MilesRoberts reopened this Oct 4, 2013
@headius
JRuby Team member
headius commented Oct 9, 2013

Hmm. Perhaps the logic for logging raised exceptions is not using the overridden stdout properly. Investigating.

@headius
JRuby Team member
headius commented Oct 9, 2013

Ok, it's strange that MRI's output is ordered like that, but I have a fix for JRuby that orders things like you'd expect:

system ~/projects/jruby17 $ cat blah.rb
log = File.new('test_log.log', "w")
$stderr.reopen log
$stderr.puts "writing to log!"
warn "warning!"

require 'some_broken_thing'

system ~/projects/jruby17 $ jruby blah.rb 

system ~/projects/jruby17 $ cat test_log.log 
writing to log!
warning!
LoadError: no such file to load -- some_broken_thing
  require at org/jruby/RubyKernel.java:1082
  require at /Users/headius/projects/jruby17/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:53
   (root) at blah.rb:6

Will commit after I come up with a test case for it.

@headius headius closed this in b92489e Oct 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.