No logs and `undefined method [] for nil:NilClass`, Rails 7
Hi, I'm unable to see HTTP logs on my Rails 7 app. I added version 1.5.0 of the gem in the :development group of my Gemfile and added the following to my /config/environments/development.rb :
HttpLog.configure do |config|
config.logger = Rails.logger
end
When I make a test http call from the ruby console using Ethon, the call succeeds but I get the following error message which points here:
/Users/<me>/.rvm/gems/ruby-3.0.3@hubflo/gems/httplog-1.5.0/lib/httplog/adapters/ethon.rb:33:in `perform': undefined method `[]' for nil:NilClass (NoMethodError)
When the test http call is made using Httparty, I don't get any error message. However regardless of the http gem used, no logs are displayed in my server logs.
I don't know what I'm missing. Thanks for your help!
Hm. Could I get your exact rails and ruby versions please, and ideally your Gemfile.lock?
Never mind, I can reproduce it somewhat. It seems to be the config.logger = Rails.logger directive, without that it works fine with Net::HTTP, with it it raises a similar error. I'll take a closer look at what has changed.
@pierrea if you move the httplog configuration from environments/development.rb into an initializer, it should work. Seems like the logger is not yet initialized at this point. To use it only in development, you can add a if Rails.env.development? guard clause.
I will update the README accordingly.
Thank you @trusche for taking the time to look into this. After moving the init code from environments/development.rb to an initializer, I still don't get any logs.
I was using a config.after_initialize block in the development.rb config file like so, which I believe is the same as putting it in a initializer:
config.after_initialize do
HttpLog.configure do |config|
config.logger = Rails.logger
config.color = { color: :black, background: :yellow }
end
end
My exact Rails version is 7.0.2.3 and ruby is 3.0.3-p157
Ok, some more questions then:
- Do you still get the exception with Ethon?
- You don't get any logs with Ethon, or HTTParty, or both?
- Could you confirm that you don't see anything in the logfiles themselves, as opposed to the output from the development server in the console? They're not always identical.
Thanks again for your willingness to dig deeper. I'm relieved to say that [httplog] lines are present in the development.log file! Do you know what could cause logs not to show in the server console? I didn't know this was possible.
Logs are present in the logfile for both Ethon and HTTParty. And yes, still getting the exception with Ethon, but no exception with HTTParty.
I just realized removing the config.logger = Rails.logger line fixes the issue! No need to specify this anymore in Rails 7, perhaps? Anyways, thank you for your help!
I think the Ethon exception is somewhat unrelated.