How capture console error messge for logging?

G

Greg Willits

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
 
G

Greg Willits

Greg said:
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
 
W

W. James

Greg said:
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'truby -e"puts X" 2>data
end

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

Thanks.

-- greg willits

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

--
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top