logging in model

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

When I try to use logger in a model, I get a NameError:

for example,

class ItemFactory

where is "< ActiveRecord::Base" ?

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

Bob/Raghu,

Thanks for your responses…

The ItemFactory model isn’t an ActiveRecord. I wonder if that could be the problem?

require ‘logger’

This seems like a good idea, but it doesn’t work. I get the same error.

What does work, I’ve discovered, is using RAILS_DEFAULT_LOGGER, as in

class ItemFactory
def getItemById(id)
RAILS_DEFAULT_LOGGER.info(“getItemById #{id}”)
end
end

Seems odd, I think. BTW, I’m using Rails 1.2.3.

– John