Project

General

Profile

Feature #14141

Add a method to Exception for retrieving formatted exception for logging purpose (Exception#{formatted,display})

Added by sorah (Sorah Fukumori) 10 months ago. Updated 10 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:83967]

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.

2.patch (8.74 KB) 2.patch sorah (Sorah Fukumori), 11/29/2017 01:04 PM
0001-Add-Exception-formatted-to-get-a-formatted-string.patch (8.36 KB) 0001-Add-Exception-formatted-to-get-a-formatted-string.patch sorah (Sorah Fukumori), 11/30/2017 10:12 AM
0002-Add-Exception-display-to-log-exception.patch (1.99 KB) 0002-Add-Exception-display-to-log-exception.patch sorah (Sorah Fukumori), 11/30/2017 10:12 AM

Related issues

Related to Ruby trunk - Bug #14324: Should Exception#full_message include escape sequences?Closed

Associated revisions

Revision 0d771889
Added by sorah (Sorah Fukumori) 10 months ago

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61154 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 61154
Added by sorah (Sorah Fukumori) 10 months ago

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]

Revision 61154
Added by sorah (Sorah Fukumori) 10 months ago

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]

History

#1 [ruby-core:83972] Updated by matz (Yukihiro Matsumoto) 10 months ago

Do you really want display that prints to IO or some other method that generates a formatted string?

Matz.

#2 [ruby-core:83973] Updated by sorah (Sorah Fukumori) 10 months ago

At least that satisfies my need. I think advanced error logging can be achieved by existing Exception#{class,message,backtrace}.

#3 Updated by sorah (Sorah Fukumori) 10 months ago

  • File 1.patch added

#4 Updated by sorah (Sorah Fukumori) 10 months ago

#5 Updated by sorah (Sorah Fukumori) 10 months ago

  • File deleted (1.patch)

#8 Updated by sorah (Sorah Fukumori) 10 months 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})

#9 [ruby-core:84003] Updated by sorah (Sorah Fukumori) 10 months ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Status changed from Open to Assigned

#10 [ruby-core:84010] Updated by Eregon (Benoit Daloze) 10 months 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).

#11 [ruby-core:84023] Updated by sorah (Sorah Fukumori) 10 months 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...

#12 [ruby-core:84079] Updated by matsuda (Akira Matsuda) 10 months 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

#13 [ruby-core:84182] Updated by akr (Akira Tanaka) 10 months ago

How about Exception#long_message or full_message ?

#14 [ruby-core:84183] Updated by sorah (Sorah Fukumori) 10 months 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.

matz (Yukihiro Matsumoto): please approve.

#15 [ruby-core:84185] Updated by matz (Yukihiro Matsumoto) 10 months ago

Hi,

full_message is approved.

Matz.

#16 Updated by sorah (Sorah Fukumori) 10 months 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]

#17 Updated by Eregon (Benoit Daloze) 9 months ago

  • Related to Bug #14324: Should Exception#full_message include escape sequences? added

Also available in: Atom PDF