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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060813/4add1dbd/attachment.html
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 Nasir Khan 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. > ------------------------------------ > > require ''logger'' > class Cart > > def add_product(product) > logger.info("Searching for product #{product.id > <http://product.id>}") <------------DOES NOT WORK > item = @items.find {|i| i.product_id = product.id <http://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 > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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. On 8/12/06, Tamim Azizadah <taz@tmpanime.com> wrote:> > 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 > > > > > Nasir Khan 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. > > ------------------------------------ > > > > require ''logger'' > > class Cart > > > > def add_product(product) > > logger.info("Searching for product #{product.id > > <http://product.id>}") <------------DOES NOT WORK > > item = @items.find {|i| i.product_id = product.id <http://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 > > > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- I don''t like $_, actually the only place I like $s is in my wallet. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060813/3fac86e7/attachment-0001.html
On Sat, Aug 12, 2006 at 07:48:43PM -0700, Nasir Khan 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