Catching all that goes to $stderr

P

Pavel Smerk

Hello,

how can I catch all output going to $stderr (e.g. to send some warning
emails time to time in case of daemon etc.)?

Thanks,

P.
 
S

Sean O'Halpin

Hello,

how can I catch all output going to $stderr (e.g. to send some warning
emails time to time in case of daemon etc.)?

Thanks,

P.
Something like this?

ioproxy = STDERR.dup

def ioproxy.write(*args, &block)
p args
#STDERR.write(*args, &block) # if you want to pass it on to real STDERR
end

$stderr = ioproxy
puts "hello"
$stderr.puts "error!"
puts "world"
warn "oops!"

__END__
hello
["error!"]
["\n"]
world
["oops!"]
["\n"]

The newlines are because puts prints its args + newline. You could
easily filter that in the write method.

Regards,
Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top