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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/30/07, Identry <identry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I try to use logger in a model, I get a NameError: > > for example, > > class ItemFactorywhere is "< ActiveRecord::Base" ?> def getItemById(id) > logger.info("getItemById #{id}") > end > end > > produces... > > NameError: undefined local variable or method `logger'' for > #<ItemFactory:0x327362c>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---