How can I access the Rails logger from an initializer? - ruby-on-rails

Following the advice from my previous question, I placed my background process in an initializer named scheduler.rb. However, I'm having a hard time getting the newly-scheduled processes to log to the Rails logs. Is there a simple way for me to access the same logs from the initializer, preferably by accessing Rails' default logger methods (logger.info, etc)?

Rails 3-
Simply use Rails.logger in your initializer
Rails.logger.info "blabla"
HTH

RAILS_DEFAULT_LOGGER was deprecated in Rails 3. The following steps work for me in Rails 3.1.
Set your logger in environment.rb before calling initialize! on your application:
Rails.logger = Logger.new(STDOUT)
MyServer::Application.initialize!
Then call the logger in your initializer.
Rails.logger.info "Hello, world!"

RAILS_DEFAULT_LOGGER.info "abc"

Related

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

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
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

Ruby Monkey Patched Logger does not work in Rails Console

I have some helper methods on my ActiveSupport::BufferedLogger such as displaying KeyValue pairs, it all works fine from rails s but fails in rails c
In rails s, I can see that my Logger extends BufferedLogger and that is the class I have chosen to MonkeyPatch, I have also tested this out with ActiveSupport::Logger as well.
I had thought that rails c ran through the same initializers that rails s used, am I wrong in thinking that?
Do I need to run some sort of initializer when starting rails c?
Location of my file is:
config/initializers/active_support_logger.rb
Error is here:
Sample Snippet listed here
class ActiveSupport::BufferedLogger
def kv(key, value)
info('%-50s %s' % ["#{key}: ".brown, value])
end
def line
info(##l.brown)
end
def block(message)
line
info(message)
line
end
end
I recommend subclassing ActiveSupport::Logger (or ActiveSupport::BufferedLogger if you really want though it's deprecated in Rails 4) instead of monkey-patching. Rails provides a configuration option to override the logger instance used by the app. This works in any context that loads the Rails environment, including the server and console.
There are a number of ways you can do this; a quick and dirty way to get you started is to just define the class and set the logger instance in an initializer that will get loaded during the initialization process of the Rails environment:
# config/initializers/my_logger.rb
class MyLogger < ::ActiveSupport::Logger
# additional methods and overrides
end
Rails.logger = MyLogger.new(Rails.root.join("log", "#{Rails.env}.log"))
For more info, check out the Rails guide to debugging applications: http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger

Logging in a gem that is used both inside and outside rails

What's a proper approach to this: I've got a gem that can be used both with or without rails - if it is used within rails, I'd like to re-use rails-services e.g. the configured logger. If it is outside of rails (e.g. in a sinatra-app), I want to fall back on the Logger from stdlib: What is a safe way for a gem to determine, if we're in a rails-app or not?!
I've tried approaches like:
#logger = defined?("Rails") ? eval("Rails.logger") : Logger.new(STDOUT)
But this only gives me an "uninitialized Constant"...
So close! Drop the quotes / eval:
#logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
I'd also encourage that you provide a config hook where the user can specify a Logger-compatible log sink. Defaults are nice but sometimes you need to configure things.

How do I output a Rails logger in Delayed_Jobs?

I would like to log #cf in this delayed_job :
(CardReferral.all.map(&:email).map(&:downcase) - CardSignup.all.map(&:email).map(&:downcase)).each do |cf|
#cf = CardReferral.find_by_email(cf)
# <--- I want to add a Rails logger here
Notifier.deliver_referred_magic_email(User.find(#cf.user_id), #cf.email, #cf.name, #cf.message, subject, editor1)
end
Rails version in Rails 2.3.5 .
Any ideas?
The logger is accessible through the "logger" command in your models and controllers. You can call one of its methods to specify your message's log level (debug, info, warn, error, fatal), like this:
logger.debug("This will be logged")
You can find more in the Rails guides here.
It actually doesn't, add this in your task:
logger = Logger.new(STDOUT)

Rails logger messages test.log?

Is it possible to configure rails to show logger.debug messages (from logger.debug statements inside controllers) to display inside test.log (or to the console) when running unit and functional tests?
I added the following to test_helper.rb. I see messages from logger.debug statements directly inside tests but no messages from logger statements inside controller methods?
def logger
RAILS_DEFAULT_LOGGER
end
Works fine for me. Have you set your config.log_level to something specific?
Eitherway,
config.log_level = :debug
should work?
From Rails 3.2 upwards the method in test_helper.rb should look like this:
def logger
Rails.logger
end

Resources