logger in google-cloud-logging is not working for rails

628 Views Asked by At

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

1

There are 1 best solutions below

1
On

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