How do I output a Rails logger in Delayed_Jobs? - ruby-on-rails

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)

Related

How to write log inside my rubygem that will be used in a rails app?

I'm developing a rubygem that will be used in a rails app. In this rubygem i need to log some information (warnings, errors...).
I saw some gems to do this, like logging, but apparently i need no configure the log output (stdout, some file).
My rails app log the messages in a file. So, my question: Is there a way to my gem use the same log configuration that my rails app uses? or my gem will send the log according to his own configuration?
You may use Rails.logger directly, which is valid if your gem will always only be used within a Rails application. You may alternatively define a logger for your gem namespace and default to Rails.logger if defined, or Logger.new(STDOUT) if it's not, along with a writer, so it's overridable:
module MyGem
def self.logger
##logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end
def self.logger=(logger)
##logger = logger
end
end
Whatever the case, you will use it like this:
MyGem.logger.debug "it works"

How to properly use logger and how to use the same logger in both Ruby and Rails

Sorry if this a very basic question, but after I checked a lot entries here (and tried to implement solutions) I still do not have an answer...
The goal is to have a ruby class which could be used withot editions (or with as little editions as possible) in both ruby scripts and as ruby on rails model. I want to have the following
logger.info 'some tracing'
However when I put such statement in ruby class I'm getting error messages - unknow method logger in both ruby and ruby on rails. Well. when I modify it such way
Rails.logger.info 'some tracing'
It starts to work in rils, but of course does not work in ruby without rails. What I'm doing wrong?
I read this http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger but I still confused, since examples from the section 'send messages' do not work.
So_ sorry again, but I see no other possibility as to connect to "the hive mind" ans ask for help ;-)
Rails uses the ruby logger by default.
Take a look at the API doc here : Logger (Ruby 2.0)
Here is an example:
require 'logger'
logger = Logger.new(STDOUT)
logger.level = Logger::WARN
logger.debug("Created logger")
logger.info("Program started")
logger.warn("Nothing to do!")
EDIT
Usage:
require 'logger'
class MyClass
def initialize
#logger = logger = Logger.new(STDOUT)
#logger.level = Logger::WARN
end
def say_something
#logger.info("You can't see me I'm not important enough")
#logger.warn("I'm visible")
end
end

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 get Rails to log my Rack::CommonLogger messages?

I am using the ruby gem rest-client with rest-client-components.
Rest-client-components enables request logging with Rack::CommonLogger.
The instructions for enabling it make use of STDOUT:
require 'restclient/components'
RestClient.enable Rack::CommonLogger, STDOUT
This works fine in development, but when I'm in production with Apache/Passenger (mod_rails), I don't see any messages from rest-client in production.log. Is there a way to integrate Rack::CommonLogger with the Rails log? Or at least to write it to a file? The former is more useful because it's easy to see the context, but the latter is better than nothing.
Thanks.
Here's the solution I came up with.
Thanks to #crohr for pointing me in the right direction.
First, create a new Logger class. Rails defaults to ActiveSupport::BufferedLogger, so we'll extend that.
# lib/rest_client_logger.rb
class RestClientLogger < ActiveSupport::BufferedLogger
def write(msg)
add(INFO, msg)
end
end
Then tell Rails to use your new logger.
# application.rb
log_file = File.open("log/#{Rails.env}.log", 'a')
log_file.sync = true # turn on auto-flushing at the file level so we get complete messages
config.logger = RestClientLogger.new(log_file)
config.logger.auto_flushing = !Rails.env.production? # turn off auto-flushing at the logger level in production for better performance
Finally, tell rest-client to use your new logger.
# config/initializers/rest_client.rb
RestClient.enable Rack::CommonLogger, Rails.logger
Limitations:
If you're using Rack::Cache with rest-client-components, this doesn't capture the cache messages.

How can I access the Rails logger from an initializer?

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"

Resources