Issue #14141 has been updated by sorah (Sorah Fukumori).


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: please approve.

[DeveloperMeeting20171212Japan]: https://bugs.ruby-lang.org/projects/ruby/wiki/DevelopersMeeting20171212Japan

----------------------------------------
Feature #14141: Add a method to Exception for retrieving formatted exception for logging purpose (Exception#{formatted,display})
https://bugs.ruby-lang.org/issues/14141#change-68292

* Author: sorah (Sorah Fukumori)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
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)
0001-Add-Exception-formatted-to-get-a-formatted-string.patch (8.36 KB)
0002-Add-Exception-display-to-log-exception.patch (1.99 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>