ridiculous / object_tracker

Track method calls to ruby objects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Track method calls to ruby objects

License:MIT License


Languages

Language:Ruby 100.0%