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) almost 7 years ago. Updated almost 7 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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0