0

I'm currently working on the GCP and deployed my rails app on the GCP instance. I'm currently using https://github.com/brendeschuijmert/google-cloud-ruby/tree/master/google-cloud-logging for the compute engine.

When I use this on the rails application.

Rails.logger.info "Hello World" works well.

But logger.warn "Hola Mundo" is not working.

I want someone shade some light on this problem.

Thanks

  • You mean `Rails.logger.warn` isn't working? or you're trying to use the command with the `Rails` prefix? – Vidur Oct 13 '20 at 07:06
  • Thank you @Vidur. `Rails.logger.warn` is also working well. But `logger.warn` is not working. I'm not sure what the issue is. – Bren De Schuijmer Oct 13 '20 at 17:29
  • What is the error message that you get? and where are you trying to call the logger from - a controller/model? – Vidur Oct 14 '20 at 03:14
  • I'm trying to use logger inside the `application_controller.rb`. And I can't get any error when I use `logger` instead of `Rails.logger` – Bren De Schuijmer Oct 15 '20 at 10:30
  • Did you ever have the chance to check the Cloud Logging library [1] for Ruby provided by Google? There are some Ruby samples for Stackdriver [2]. [1] https://cloud.google.com/logging/docs/setup/ruby [2] https://github.com/GoogleCloudPlatform/ruby-docs-samples/blob/d89b8285ba5d939aefd5bae7070522ebcdfef3b8/stackdriver/Gemfile – Carlo C. Oct 19 '20 at 11:38
  • Yes, it's still not working. This issue is related to https://github.com/googleapis/google-cloud-ruby/issues/7644 – Bren De Schuijmer Oct 20 '20 at 12:11
  • A comment on the GIT issue you found: https://github.com/googleapis/google-cloud-ruby/issues/7644 To add to what user "mattes" commented there would be useful – Carlo C. Oct 21 '20 at 12:40

1 Answers1

1

If you're trying to call logger from outside of a controller/model or some other file that is a part of Rails' structure - you will have to explicitly create a logger for yourself with:

logger = Logger.new(STDOUT) # Or wherever you want to log to

However if Rails.logger is working, then you likely just need an alias like logger = Rails.logger to allow you to use logger.warn without the Rails namespace prefix. You're probably in a file that doesn't already have a helper that is aliasing that for you.

Some more digging in the API reveals that the logger stems from ActiveSupport::LogSubscriber - several classes like ActionController include child LogSubscribers that inherit from that module and hence have the logger method available to them. You can manually alias it to Rails.logger to have it work for you wherever you are trying to invoke it.

Source: https://api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html#method-c-logger

Vidur
  • 1,442
  • 2
  • 17
  • 37
  • Thank you Vidur!, please allow me to add something to this. I think we can read more about this on: https://stackoverflow.com/questions/17749804/using-logger-in-rails-4 And sections 2.2 & 2.3 of this Ruby documentation: https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels Also a comment on the GIT issue they found: https://github.com/googleapis/google-cloud-ruby/issues/7644 To add to what user "mattes" commented there would be good. – Carlo C. Oct 21 '20 at 12:39