Logging in Rails

This is a newbie question, I have a class which is not derived from
ActionController or ActiveRecord but I want to use logging, I tried
require
but still logging does not work -
This class is located in a file in “model” directory.

require ‘logger’
class Cart

def add_product(product)
logger.info(“Searching for product #{product.id}”)
<------------DOES
NOT WORK
item = @items.find {|i| i.product_id = product.id}
logger.info(“Item to be added is #{item.product_id}”)
<------------DOES
NOT WORK
if item
item.quantity += 1
else
@items << LineItem.for_product(product)
end
@total_price += product.price
end

end


Thanks

On Sat, Aug 12, 2006 at 07:48:43PM -0700, Nasir K. wrote:

This is a newbie question, I have a class which is not derived from
ActionController or ActiveRecord but I want to use logging, I tried require
but still logging does not work -
This class is located in a file in “model” directory.

s/logger/RAILS_DEFAULT_LOGGER/ should do the trick.

  • Matt

I’m totally new to Ruby and Rails, but I believe this is the wrong place
to put logging. Logging is not part of the model. This should be added
in one of your controllers where some action is actually happening. You
should have the logging in the controller that’s calling the add_product
method. So say you have some controller called store_controller in your
controllers directory, you can add something like this:

def add_to_cart
begin
@product = Product.find(params[:id]) # you get the id when the
person clicks the button and does a POST
rescue
logger.error(“Attempt to access invalid product #{params[id]}”)
flash[:notice] = “Invalid product”
redirect_to :action => :index
else
@cart = find_cart
@cart.add_product(@product)
end
end

Thanks for your reply but I dont think this could be correct.
Logging should be possible to be added anywhere, I could always write my
own
logging but I am sure there is way to use the default logger.