Simple and perfect check-in at Sinatra

I looked at several blogs and websites that gave me some information on how to log in to synatra, but didn’t work for my application, and I also went through a gem called sinatra-logger, I didn’t try, I wanted to know a perfect and easy way " enter "into the sinatra. Like logger.info for rails.

The code I tried and didn’t work is located from the following link site, and also some of the SO links too pointed to the same approach in the link above.

configure do LOGGER = Logger.new("sinatra.log") end helpers do def logger LOGGER end end 

I wrote this in the app.rb of my application, it does not say the undefined `configure 'method for App: Module

Any pointers or guidance are helpful. Thanks.

edits Right. Now I use the file entry to record my errors and messages:

 File.open('log.txt', 'a') do |f| f.write "#{status}-#{CONFIG.api_url}-#{data.inspect}-tweet}" end 
+4
source share
2 answers

If you use Sinatra 1.3, you can log in the same way as in rails using logger.info

Copied from Sinatra Readme :


entrance

In the request area, the log assistant detects a Logger instance:

 get '/' do logger.info "loading data" # ... end 

This logger automatically takes into account rack account management settings. If logging is disabled, this method will return a dummy object, so you don’t have to worry about your routes and filters.

Note that logging is only allowed for Sinatra :: Application by default, so if you inherit from Sinatra :: Base, you probably want to enable it yourself:

  class MyApp < Sinatra::Base configure :production, :development do enable :logging end end 

To avoid installing middleware, set the logging parameter to nil. However, keep in mind that the registrar in this case returns zero. A common use case is when you want to set up your own registrar. Sinatra will use whatever it finds in env ['rack.logger'].


Rack :: CommonLogger creates log messages inside (I think).

+18
source

See my offer here: Entrance to Sinatra?

However, this sounds out of your question, since the problem is that the application is a module.

your main Sinatra class should be something like this:

 class App < Sinatra::Base 

Unlike

 module App < Sinatra::Base 

Make sure you define it as a class and this error should go away.

+2
source

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


All Articles