
Issue #20864 has been updated by ioquatix (Samuel Williams).
I wonder why error: in particular, and not just warn(text, **any_structured_metadata)?..
I'd be okay with that, in fact I was thinking of making that proposal but I wasn't sure it would be accepted if it was too general.
Why not include the error directly in the warning message?
Good question! Unfortunately, I found that the formatting behaviour of `Warning.warn` is a bit problematic. The first argument to `Warning.warn` is a string concatenation of all arguments passed to `Kernel#warn`. Depending on the log output, I want to include the details of the error in a JSON format for structured logging. In order to do that, I need the original objects passed to `warn`. This includes adding trace metadata which may not be known about until later on in the output handling. However, I don't want to do that at the point of the call, as we don't know the output format at that point... it could be JSON, MessagePack, plain text, or some other format. It may be the case that `warn` was not designed to be used that way, and I'm okay if that's the case. But I think this is a useful and generic extension point for logging. If we can allow extra meta-data (that can be ignored by default) to go via options, it significantly increases the usefulness of this interface - we can pick up those options in `Warning.warn` and use those to control the output accordingly. ---------------------------------------- Feature #20864: Support `error:` keyword to `Kernel#warn` https://bugs.ruby-lang.org/issues/20864#change-110400 * Author: ioquatix (Samuel Williams) * Status: Open ---------------------------------------- `Kernel#warn` is extremely useful. 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 errors. I'd like to propose another extension to log errors. ```ruby begin # some work rescue => error warn "An error occurred!", error: error end ``` Such a feature would allow us to log errors using a standard interface. I don't have an opinion about the implementation, but I wanted to get feedback on the interface. -- https://bugs.ruby-lang.org/