From: Gordon Thiesfeld Date: 2007-08-25T23:35:03+09:00 Subject: Re: Logging object. On Aug 25, 8:22 am, Jeremy Wells wrote: > tobyclem...@gmail.com wrote: > > Hi, > > > Can anyone suggest the best way to pass a Logger object around between > > all the classes in an application? > > > Currently I have it set in a constant in the top-level but I don't > > feel that this is the best way to do it as it couples the code in each > > of my classes to the current implementation and makes it difficult for > > me to reuse the code elsewhere without first setting up a logger > > instance of the same name. I had a similar need, but I also needed a way to send emails from different classes as well, so the code started getting a lot of extra junk in it. I solved it by borrowing some code from zenspider's autotest. I took his idea of hooks and plugins and extracted it into a mixin, so I can include it in all of my classes. So in your toplevel file you might have: require 'hookable' require 'foo' require 'logging' require 'email' require 'other_plugins' foo.rb might look like this class Foo include Hookable def something hook :something_happened end end then in logging.rb, you would have: @log = Logger.new(...) Foo.add_hook(:something_happened){|i| @log.info "something was called"} Also, in the block, you have access to the object that called the hook. The hookable mixin code is here: http://pastie.caboo.se/90899 regards, Gordon