hasrthur
hasrthur

Reputation: 1490

Rails 3 model logger

I have a model, but I don't want it to write to the common log its queries. Is it possible to switch logger for some model?

Upvotes: 1

Views: 1137

Answers (3)

blackfoks
blackfoks

Reputation: 121

I'm using push-core gem and it writes many annoying messages to the log, such following, every 2 seconds:

Push::Message Load (0.6ms)  SELECT "push_messages".* FROM "push_messages" ...

I've added following code into an initializer in order to remove "Push::Message Load" events from the log:

# Disable annoying (every 2 sec) "Push::Message Load" events
ActiveRecord::LogSubscriber.class_eval do
  alias_method :old_sql, :sql

  def sql(event)
    return if event.payload[:name] =~ /Push::Message Load/
    old_sql(event)
  end
end

Upvotes: 1

sandeep
sandeep

Reputation: 521

You can do this:

Create a custom Logger if you want to format it differently.You can ignore this if format needs to be the same.

class ModelLogger < Logger
  def format_message(severity, timestamp, progname, msg)
    "#{timestamp.to_formatted_s:)db)} #{severity} #{msg}\n"
  end
end

In the config/initializers/ , create logs.rb and add this:

model_logfile = File.open("#{RAILS_ROOT}/log/model.log", 'a')
model_logfile.sync = true
MODEL_LOG = ModelLogger.new(model_logfile)

Now, after you restart your server,

MODEL_LOG.debug "This will be logged to model.log"
MODEL_LOG.error "Errors also can be logged."

Upvotes: 2

Hoa
Hoa

Reputation: 3207

This should answer your question. Replace STDOUT with whatever meets your needs
how to change the rails logger to use standard out from rake tasks (rails2)

Upvotes: 1

Related Questions