# How capture console error messge for logging?

**URL:** <https://www.ruby-forum.com/t/how-capture-console-error-messge-for-logging/180506>\
**Category:** Ruby\
**Created:** [December 20, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/how-capture-console-error-messge-for-logging/180506 "2009-12-20T21:28:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [December 20, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/how-capture-console-error-messge-for-logging/180506/1 "2009-12-20T21:28:41Z")

</div>

OS X, Ruby 1.8

I’ve been searching & reading, but not finding (or perhaps just not  
understanding) how I can capture as a string the exact same error  
message text that would normally get dumped to Terminal.

So, if I write this to generate an error:

puts “hello”  
bogus\_command

I’ll get a file name, line number, and description of an error dumped to  
the Terminal.

How do I snag all that text so I can add it to a log file?

Obviously, I’d start with a begin/rescue and inside rescue I’d do  
something. I’ve fiddled with some examples I’ve seen but nothing is  
doing what I want. I just want that same text captured as a string into  
a variable I can then use however I want.

begin  
puts “hello”  
bogus\_command  
rescue

# now what?

# I was hoping this would work

# x = $stderr.print

# but it doesn’t

end

I’m sure it’s some standard \*nix thing, but I’m not getting it.

Thanks.

– greg willits

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [December 20, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/how-capture-console-error-messge-for-logging/180506/2 "2009-12-20T21:42:09Z")

</div>

Greg W. wrote:

> I’ve been searching & reading, but not finding (or perhaps just not  
> understanding) how I can capture as a string the exact same error  
> message text that would normally get dumped to Terminal.
> 
> begin  
> puts “hello”  
> bogus\_command  
> rescue
> 
> # now what?
> 
> # I was hoping this would work
> 
> # x = $stderr.print
> 
> # but it doesn’t
> 
> end

DOH!! As soon as I hit send, I realized I could try one more thing:

x = $!

That gave me the error message. And then a key word popped into my head.  
I wanted the “stack trace.” More googling gave me the .backtrace method.

So now I have:

begin  
puts “hello”  
bogus\_command  
rescue=\> err  
x = “#{err.backtrace[0] #{err}}”  
end

This works perfectly. So, unless there’s A Better Way someone wants to  
suggest, I think I am good.

thanks for listening 🙂

– greg

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [December 20, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/how-capture-console-error-messge-for-logging/180506/3 "2009-12-20T21:50:46Z")

</div>

Greg W. wrote:

> begin
> 
> Thanks.
> 
> – greg willits

ruby -e"puts X" 2\>errors.log
