Open caius opened 6 months ago
I had this wired up in a Rails 6.1 & 7.0 app by adding the following to the config/boot.rb
file:
require "oas_agent" # Load O&S Agent before other dependencies
And then in config/application.rb
inside the application class definition I had
# Enable ruby warnings if OAS is wired up in this environment
if OasAgent::AgentContext.config[:enabled]
# Same as setting RUBYOPT="-W2" in environment
$VERBOSE = true
Warning[:deprecated] = true
end
Been experimenting in our application with not adding
RUBYOPT=-W2
but trying to enable theWarning
categories at runtime instead. Makes it easier for me to dis/enable warnings per-environment for … Reasons™.https://www.fastruby.io/blog/exploring-ruby-warnings.html has been quite useful in figuring this out as well. Seems it's not enough to just have the categories enabled, but
$VERBOSE
needs to be set totrue
as well.These are all run on Ruby 3.2 for now (Ruby 3.3 brings a
:performance
category in as well.) To see whetherWarning.warn
has been called, I'm overwriting it directly to dump output. To trigger a warning, we're defining thefoo
method twice as well at the end.Starting with defaults, we see no ruby warnings emitted:
Adding
-W2
to the mix does what we expect (and tells us we've redefinedWarning.warn
too). We get warnings output forfoo
method redefinition, and also see both Warning categories and$VERBOSE
get set to true.Perhaps we can replicate this at runtime? First try setting just the Warning deprecated category to true, we want warnings in that category to be output after all. We observe the category being changed but no warning is output.
Given
$VERBOSE
is mentioned in Fast Ruby's blog post and-W2
sets it to true, what if we also set that and the deprecated category at runtime. Seems that's the magic trick, we end up with warnings being output where we expect without having to pass-W2
at invocation time.