On Aug 25, 3:34 pm, Gordon Thiesfeld <gthiesf... / gmail.com> wrote: > On Aug 25, 8:22 am, Jeremy Wells <jwe... / servalsystems.co.uk> 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 Thanks for all of your help. I'm having a bit of trouble getting into the swing of OO properly - I mean I've read all the theory but in practice there's still a lot to think about! I think it's mostly the design patterns that I need to get used to as the actual technicalities of a language aren't so difficult to pick up. Anyhow, I think by picking bits from the methods suggested here I'll be able to solve my problem. One other thing I was wondering about was my testing. How does one test that a log has actually been written to without writing to file but I was thinking if I have an object of a class that acts as a dummy logger that can also work as a real logger if required, I can query that object to see whether the log was actually written. I like the idea of using hooks because then multiple actions can occur when a certain methods performs a certain task. Another quick question, slightly off topic but something I've been thinking about with regard to this: If I have a test that checks the status of a logger object to determine whether something is working correctly in the class under test, is this a functional test or a unit test? Thanks, Toby Again, thanks very much, Toby