Re: logging in model

raghukumar
2007-10-30T18:11:48Z

require ''logger''

class ItemFactory < Active::Base

   def initialize(log_file)
        @@logger = Logger.new(log_file)
   end
   def getItemById(id)
     @@logger.info("getItemById #{id}")
     # write what ever code you want
     find(id)
   end
end

Regards,
Raghu KUmar K


On Oct 30, 6:35 pm, Identry
<iden...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> When I try to use logger in a model, I get a NameError:
>
> for example,
>
> class ItemFactory
>   def getItemById(id)
>     logger.info("getItemById #{id}")
>   end
> end
>
> produces...
>
> NameError: undefined local variable or method `logger'' for
> #<ItemFactory:0x327362c>
>     /Users/jalmberg/rails/tstools/config/../app/models/item_factory.rb:3:in
> `getItemById''
>     test/unit/company_test.rb:17:in `test_item_factory''
>
> I get the same kind of error when I try to use logger in unit tests, like:
>
> def test_item_factory
>     item = itemFactory.getItemById(target_lot)
>     assert_equal item[''lot_number''], target_lot
>     logger.info("found lot #
#{item[''lot_number'']}")
>   end
>
> What am I doing wrong? Do I need to import the logger object into the model
> or test namespace? Some other trick?
>
> TIA: John