0.0
No release in over 3 years
Low commit activity in last 3 years
Track method calls to any object. Class and instance methods can be tracked (w/ arguments and source location).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 2.2.11
>= 12.3.3
 Project Readme

Ruby Object Tracker Gem Version

An easy way to track Ruby objects. Logs class and instance method calls with arguments, file name, line number and execution time. Helpful for debugging and learning the language.

Requirements

  • Ruby 2+

Installation

Add this line to your application's Gemfile:

gem 'object_tracker', '~> 2.1'

Usage

class Person
  extend ObjectTracker

  def greet(name)
    "ello, #{name}!"
  end
end

Track a single method:

Person.track_all! :greet

Or track all methods (such as to_s, object_id, respond_to?, etc):

Person.track_all!

Or track an instance:

obj = Person.new.extend ObjectTracker
obj.track_all!

Or track an object without extending the class:

ObjectTracker.(Person)
ObjectTracker.(Person, :greet)
ObjectTracker.(Person, except: :respond_to?)
ObjectTracker.(me = Person.new)

Hook methods

Pass a proc (or anything that responds to #call) to the :before or :after options and they will be called before and after the method call, respectively. This allows you to do things like track the number of methods calls.

method_calls = Hash.new 0
slow_methods = Set.new []
hooks = [
  before: ->(context, name, args) { method_calls[name] += 1 },
  after: ->(context, name, args, duration) { slow_methods << name if duration > 0.05 }
]
ObjectTracker.(Person, *hooks)

Logging

ObjectTracker uses the default Ruby logger with a default DEBUG level

Silence logging

ObjectTracker.logger.level = Logger::ERROR

[Example] ActiveRecord Tracking

Tracking an ActiveRecord 3.2 model

ObjectTracker.(u = User.first).audits;nil
#=>   User Load (0.3ms)  SELECT `users`.* FROM `users` LIMIT 1
#=> [2017-04-13T21:45:48.827952]  INFO -- ObjectTracker: following #<User:0x007f838d8cf560>
#=> [2017-04-13T21:45:48.828223] DEBUG -- ObjectTracker: User#class [RUBY CORE] (0.00000)
#=> [2017-04-13T21:45:48.838797] DEBUG -- ObjectTracker: User#association with [audits] [lib/active_record/associations.rb:155] (0.01073)
#=> [2017-04-13T21:45:48.838853] DEBUG -- ObjectTracker: User#audits [lib/active_record/associations/builder/association.rb:43] (0.01082)

Troubleshooting

Having problems? Maybe a specific method is throwing some obscure error? Try ignoring that method, so we can get back on track!

Person.track_all! except: :bad_method

Contributing

License

MIT