Log All Calls To Instance Methods Of A Ruby Class

We came across a situation where we lost track of the timing of execution of certain pieces of code.  To help us debug, we wanted to log every time the methods of a particular class were called, but without junking up the existing methods with repetitive calls to the logger.

We created this handy module to save us some time, and keep things clean. Here’s our Rails compatible method logger, which could be used to wrap all instance methods of a class with other code if desired:

Use this to log all calls to instance methods of a class to the log.  Just include it at the top of your class like so:

Then the next time you call any function in your class like so:

Then you will see a nice output in your log: