Error "undefined local variable or` logger 'method "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 using ruby -rubygems app.rb When I go to http: // localhost: 4567 , I get the error message:

 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? By reading the Sinatra README and the documentation, it seems that logging is enabled by default for Sinatra::Application .

+4
source share
3 answers

You are probably missing logger = Logger.new .

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

+1
source

The problem is the method of the record not found, just extend the Logger class this way, and everything should be fine:

 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 
+4
source

The logger is not defined, to overcome it you can simply use

 Rails.logger.info "COUCOU" 

or define it as follows:

 logger = Rails.logger.new logger.info "COUCOU" 
0
source

Source: https://habr.com/ru/post/1346628/


All Articles