Feature #14141
closedAdd a method to Exception for retrieving formatted exception for logging purpose (Exception#{formatted,display})
Description
Most people want to log caught exceptions to stderr (or somewhere else) then continues their program as usual.
def the_program
# ...
raise "failure!"
# ...
rescue RuntimeError => e
$stderr.puts "#{e.message} (#{e.class})\n\t#{e.backtrace.join("\n\t")}"
retry
end
I'm very bored to write error logging many time...
I want to log errors in the default format of Ruby, just like the following:
rescue RuntimeError => e
e.display
# ...
From Ruby 2.5, we've started branching error formatting on TTY-ness of $stderr
. It'd be bit more useful if we can log using the same format with the format which Ruby determines.
Ruby already has Object#display
.
One consideration is to retrieve formatted String from Exception object, but the current error logging code (eval_error.c) depends on IO,
so I want to start from just having Exception#display
. I think most use case is just to log errors into IO.
Files
Updated by matz (Yukihiro Matsumoto) about 7 years ago
Do you really want display
that prints to IO
or some other method that generates a formatted string?
Matz.
Updated by sorah (Sorah Fukumori) about 7 years ago
At least that satisfies my need. I think advanced error logging can be achieved by existing Exception#{class,message,backtrace}.
Updated by nobu (Nobuyoshi Nakada) about 7 years ago
Kernel#display
outputs to STDOUT
by the default.
https://github.com/nobu/ruby/tree/feature/Exception%23display
Updated by sorah (Sorah Fukumori) about 7 years ago
- File 0001-Add-Exception-formatted-to-get-a-formatted-string.patch 0001-Add-Exception-formatted-to-get-a-formatted-string.patch added
- File 0002-Add-Exception-display-to-log-exception.patch 0002-Add-Exception-display-to-log-exception.patch added
Hmm. Changed my mind. Implemented Exception#formatted
with this patch.
I'm not sure this name is proper, suggestions appreciated
Updated by sorah (Sorah Fukumori) about 7 years ago
- Subject changed from Exception#display to display same formatted text for IO to Add a method to Exception for retrieving formatted exception for logging purpose (Exception#{formatted,display})
Updated by sorah (Sorah Fukumori) about 7 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by Eregon (Benoit Daloze) about 7 years ago
+1 on an easy way to get a formatted exception.
BTW, "#{e.message} (#{e.class})\n\t#{e.backtrace.join("\n\t")}"
is slightly different than what the C code does, if the message spans multiple lines (then the (#{e.class}) is still on the first line).
Updated by sorah (Sorah Fukumori) about 7 years ago
is slightly different than what the C code does, if the message spans multiple lines (then the (#{e.class}) is still on the first line).
meh, yes, I just gave up to write the exact Ruby code with the C-level behavior...
Updated by matsuda (Akira Matsuda) about 7 years ago
I'm not sure this name is proper, suggestions appreciated
How about "to_formatted_s"?
Active Support has this method for some core classes, and so I guess this name may sound familiar to the Rails people. https://github.com/rails/rails/blob/07788c7/activesupport/lib/active_support/core_ext/time/conversions.rb#L26-L41
Updated by akr (Akira Tanaka) about 7 years ago
How about Exception#long_message or full_message ?
Updated by sorah (Sorah Fukumori) about 7 years ago
In DeveloperMeeting20171212Japan. We concluded to add Exception#formatted
in the proposed patch, as #full_message
in Ruby 2.5
Several considerations are pointed out:
- Q. Does the method interact with
$stderr.tty?
?- Yes.
- Q. Is it imaginable?
- The motivation is to have a method to retrieve the same formatted string that Ruby prints out in the execution context at the timing of method call (TTY-ishness)
- Q. Naming
-
long_message
which follows existing#message
-
full_message
is more familiar (Rails has a similarly named method)
-
- Q. What if the method accepts some arguments to specify formatting option? (e.g., coloring, backtrace direction)
- (Proposed implementation changes formatting from
$stderr.tty?
) - I'd like to start with the minimum implementation. We can decide to add options afterward.
- (Proposed implementation changes formatting from
@matz (Yukihiro Matsumoto): please approve.
Updated by matz (Yukihiro Matsumoto) about 7 years ago
Hi,
full_message
is approved.
Matz.
Updated by sorah (Sorah Fukumori) about 7 years ago
- Status changed from Assigned to Closed
Applied in changeset trunk|r61154.
error.c(exc_full_message): Exception#full_message
Add a method to retrieve a String expression of an exception,
formatted in the same way that Ruby prints an uncaught exception out.
[Feature #14141]
Updated by Eregon (Benoit Daloze) almost 7 years ago
- Related to Bug #14324: Should Exception#full_message include escape sequences? added