Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "undefined local variable or method `logger'" when using `logger.info` in a Sinatra application

I have the following Sinatra 1.2.1 application code:

# app.rb
require 'sinatra'

get '/' do
  logger.info "COUCOU"
  'Hello world!'
end

and start the server with ruby -rubygems app.rb. When I go to http://localhost:4567 I get the error:

NameError at /
undefined local variable or method `logger' for #<Sinatra::Application:0x00000100d91f88>
file: app.rb location: block in <main> line: 4

Do I need to add or configure something to enable logging in Sinatra? Reading the Sinatra README and documentation, it looks like logging is enabled by default for Sinatra::Application.

like image 772
Florent2 Avatar asked Apr 04 '11 21:04

Florent2


2 Answers

The problem is in the not found write method, just extend the Logger class this way and everything should be ok:

class Logger
  # Make Rack::CommonLogger accept a Logger instance
  # without raising undefined method `write' for #<Logger:0x007fc12db61778>
  # makes a method alias using symbols

  alias :write :<<
end
like image 160
João Pereira Avatar answered Sep 19 '22 02:09

João Pereira


You are probably missing a logger = Logger.new.

http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/

like image 35
Michael Kohl Avatar answered Sep 18 '22 02:09

Michael Kohl