On Aug 4, 2008, at 3:35 PM, ara.t.howard wrote:

>
> On Aug 4, 2008, at 1:43 PM, Chuck Remes wrote:
>
>> [snip]
>> I can load and instantiate this only once in the runtime. If I try  
>> to do it a second time, it's like I have reopened the class/module  
>> and Logger gets redefined. Each instantiation shares the same  
>> global Logger which is *not* what I want. I want each one to have  
>> their own instance.
>>
>> Is this possible? Any recommendations for solving it?
>>
>> cr
>>
>>
>
> something similar to:
>
>
>
> module Namespace
>  module Logger
>    def Logger.key *key
>      @key = key.first unless key.empty?
>    end
>
>    def instance
>      @instances = Hash.new{|h,k| @instances[key || :default ] ||= new}
>    end
>
>     %w( debug info warn error fatal ).each do |method|
>      	module_eval <<-code
>          def Logger.#{ method }(*a, &b)
>            instance.#{ method }(*a, &b)
>          end
>        end
>      end
>   end
> end
>
>
> Namespace::Logger.key Thread.current.object_id   # or something unique
>
> Namespace::Logger.info{ 'foobar' }

For lurkers, here is some slightly corrected code (the above doesn't  
compile).

require 'logger'

module Namespace
  module Logger
    def Logger.key= *key
      @key = key.first unless key.empty?
    end

    def Logger.key
     @key
    end

    def Logger.instance
      @instances ||= Hash.new { |h,k| h[key || :default ]  
= ::Logger.new(STDOUT) }
      @instances[@key]
    end

     %w( debug info warn error fatal ).each do |method|
      	module_eval <<-code
          def Logger.#{ method }(*a, &b)
            instance.#{ method }(*a, &b)
          end
        code
      end
   end
end

Ara, thanks for the help.

cr