Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking/Logging ActiveRecord Callbacks

Is there any way to automatically log everytime an ActiveRecord callback happens? It would help to trace through why certain things are happened when a record has several callbacks in place.

I'd like to see automated log message that indicate which messages are being called in response to which callbacks, e.g.: before_validation: calling update_capitalization

like image 949
mahemoff Avatar asked Oct 26 '12 15:10

mahemoff


People also ask

What are rails callbacks?

In Rails, callbacks are hooks provided by Active Record that allow methods to run before or after a create, update, or destroy action occurs to an object. Since it can be hard to remember all of them and what they do, here is a quick reference for all current Rails 5 Active Record callbacks.

What is the log to check for errors in rails?

Rails uses six different log levels: debug, info, warn, error, fatal, and unknown. Each level defines how much information your application will log: Debug: diagnostic information for developers and system administrators, including database calls or inspecting object attributes.

What is rails logger debugging?

logger. debug , or in short logger. debug writes the parameter text to a log file. This log file is by default environment specific. If you are currently running rails in development environment then the log file in use is log/development.

Where are rails logs stored?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.


2 Answers

For google and posterity (on Rails 3):

module CallbackTrace
  def self.included kls
    kls.send :alias_method_chain, :_compile_filter, :trace
  end

  def _compile_filter_with_trace filter
    generated_code = _compile_filter_without_trace(filter)
    return generated_code if filter.is_a?(Array)
    method_name = generated_code.to_s.split(%r{\(|\s}).first
    _klass = @klass
    prelogger = ->{
        Rails.logger.info("START [#{filter.class}](#{generated_code})")
        Rails.logger.info("#{_klass} #{Time.now}")
        if imethod=(_klass.instance_method(method_name) rescue nil)
          begin
            Rails.logger.info(imethod.source)
          rescue MethodSource::SourceNotFoundError
            Rails.logger.info("NO SOURCE FOR #{generated_code}")
          end
        else
          Rails.logger.info("NO METHOD: #{method_name} for #{@klass}")
        end
    }
    postlogger = ->{
      Rails.logger.info("ENDED #{generated_code} #{Time.now}")
    }
    @klass.send :define_method, "prelogger_#{method_name}", &prelogger
    @klass.send :define_method, "postlogger_#{method_name}", &postlogger
    "(prelogger_#{method_name}; retval = retval = #{generated_code}; " +
        "postlogger_#{method_name}; retval)"
  end
end
ActiveSupport::Callbacks::Callback.send :include, CallbackTrace
like image 40
xtoddx Avatar answered Oct 15 '22 02:10

xtoddx


I just wrote a gem for this.

# Gemfile
gem "rails-callback_log", group: [:development, :test]
  • https://github.com/jaredbeck/rails-callback_log
  • https://rubygems.org/gems/rails-callback_log
like image 169
Jared Beck Avatar answered Oct 15 '22 02:10

Jared Beck