# Catching all that goes to $stderr

**URL:** <https://www.ruby-forum.com/t/catching-all-that-goes-to-stderr/100315>\
**Category:** Ruby\
**Created:** [July 28, 2006, 11:27am UTC](https://www.ruby-forum.com/t/catching-all-that-goes-to-stderr/100315 "2006-07-28T11:27:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [July 28, 2006, 11:27am UTC](https://www.ruby-forum.com/t/catching-all-that-goes-to-stderr/100315/1 "2006-07-28T11:27:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [July 28, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/catching-all-that-goes-to-stderr/100315/2 "2006-07-28T16:22:22Z")

</div>

On 7/28/06, Pavel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
