Gordon Thiesfeld
8/25/2007 1:51:00 PM
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.
>
> > Thanks,
> > Toby
>
> Surely any way you do this if a class writes to a logger you have to set
> up the logger first? But anyhow, I guess you could make a module that
> provides the logging and include that on all appropriate classes:
>
> logging.rb:
> LOG = Logger.new(...)
>
> module Logging
> def logger
> LOG || Logger.new(nil)
> end
> end
>
> something.rb:
> require 'logging'
>
> class Something
> include Logging
>
> def do_something
> logger.info "something"
> end
> end