Project

General

Profile

Actions

Feature #14141

closed

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

Added by sorah (Sorah Fukumori) over 6 years ago. Updated over 6 years ago.

Status:
Closed
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.


Files

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 1 (0 open1 closed)

Related to Ruby master - Bug #14324: Should Exception#full_message include escape sequences?Closedsorah (Sorah Fukumori)Actions

Updated by matz (Yukihiro Matsumoto) over 6 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) over 6 years ago

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

Actions #3

Updated by sorah (Sorah Fukumori) over 6 years ago

  • File 1.patch added
Actions #4

Updated by sorah (Sorah Fukumori) over 6 years ago

Actions #5

Updated by sorah (Sorah Fukumori) over 6 years ago

  • File deleted (1.patch)
Actions #8

Updated by sorah (Sorah Fukumori) over 6 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) over 6 years ago

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

Updated by Eregon (Benoit Daloze) over 6 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) over 6 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) over 6 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) over 6 years ago

How about Exception#long_message or full_message ?

Updated by sorah (Sorah Fukumori) over 6 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.

@matz (Yukihiro Matsumoto): please approve.

Updated by matz (Yukihiro Matsumoto) over 6 years ago

Hi,

full_message is approved.

Matz.

Actions #16

Updated by sorah (Sorah Fukumori) over 6 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]

Actions #17

Updated by Eregon (Benoit Daloze) about 6 years ago

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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0