# Logging in model

**URL:** <https://www.ruby-forum.com/t/logging-in-model/118735>\
**Category:** Rails\
**Created:** [October 30, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/logging-in-model/118735 "2007-10-30T14:37:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 30, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/logging-in-model/118735/1 "2007-10-30T14:37:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 30, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/logging-in-model/118735/2 "2007-10-30T15:05:17Z")

</div>

On 10/30/07, Identry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When I try to use logger in a model, I get a NameError:
> 
> for example,
> 
> class ItemFactory

where is “\< ActiveRecord::Base” ?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 30, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/logging-in-model/118735/3 "2007-10-30T19:12:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 30, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/logging-in-model/118735/4 "2007-10-30T19:42:42Z")

</div>

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
