Super simple logging for objects.
It’s a pain to trudge through large log files. Sometimes you just want to see log messages for one particular object.
class Something include ObjectLogging def foo logger.debug "entering" logger.info "leaving" end end something = Something.new something.foo puts something.logger.log # => [DEBUG] Something#foo entering # [INFO] Something#foo leaving
This logs to memory. Your log will be gone when the object goes out of scope.
class Something include ObjectLogging object_logging :rails_cache, :id => :proc_or_method_name_to_identify_object end
This is useful for when you want to view the log after the object goes out of scope.
The option :id
needs to a Proc or method name that returns a string that uniquely identifies the object (i.e. will be used as the cache key).
class Something include ObjectLogging object_logging :rails_log end class Something include ObjectLogging object_logging :stdout end
This logs to memory, but also logs to Rails.logger
or STDOUT
.
You can pass Logger instances between your objects. This is useful for when you want an object to temporarily log to another object’s log.
a = Account.new u = User.new a.logger.entries.length # => 0 u.logger.entries.length # => 0 a.logger.debug "blah" u.logger.debug "bleh" u.logger = a.logger u.logger.debug "weee" a.logger.entries.length # => 2 u.logger.entries.length # => 1
Copyright © 2010 Christopher J. Bottaro. See LICENSE for details.