[ruby-core:119785] [Ruby master Feature#20864] Allow `Kernel#warn` to accept `**options` and pass these to `Warning.warn`.
From:
"ioquatix (Samuel Williams) via ruby-core" <ruby-core@...>
Date:
2024-11-06 22:26:57 UTC
List:
ruby-core #119785
Issue #20864 has been updated by ioquatix (Samuel Williams).
According to the discussion I've updated the proposal.
----------------------------------------
Feature #20864: Allow `Kernel#warn` to accept `**options` and pass these to `Warning.warn`.
https://bugs.ruby-lang.org/issues/20864#change-110459
* Author: ioquatix (Samuel Williams)
* Status: Open
----------------------------------------
## Background
`Kernel#warn` is extremely useful, especially because there is a mechanism for loggers to redirect warnings using the `Warning` module. Currently, it is possible to include some details about the current caller using `uplevel` for the current call stack. But it is not possible to include details about exceptions.
```ruby
begin
...
rescue => error
warn "Something went wrong!", exception: error
end
```
Because of this, trying to emit detailed structured logs from `warn` is tricky.
## Proposal
I'd like to propose another extension to log errors. We would allow `warn(..., **options)` to accept all options, and forward them all to `Warning.warn`.
This would allow us to add more details to warnings and emit structured logs using `Warning.warn`.
A simple example of the proposed interface:
```ruby
module Kernel
def warn(*arguments, uplevel: ..., **options)
# Existing processing of arguments -> message
::Warning.warn(message, **options)
end
end
```
Current behaviour rejects any unknown options:
```
warn("Oops", exception: error)
# => <internal:warning>:50:in `warn': unknown keyword: :exception (ArgumentError)
```
I don't have an opinion about the implementation, but I wanted to get feedback on the interface.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/