Simple and Ideal Logging in Sinatra

那年仲夏 提交于 2019-12-03 16:18:46
Sean Lerner

If you are using Sinatra 1.3 you should be able to log just like in rails with logger.info

The following is copied from the Sinatra Readme:


Logging

In the request scope, the logger helper exposes a Logger instance:

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

This logger will automatically take your Rack handler’s logging settings into account. If logging is disabled, this method will return a dummy object, so you do not have to worry in your routes and filters about it.

Note that logging is only enabled 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 any logging middleware to be set up, set the logging setting to nil. However, keep in mind that logger will in that case return nil. A common use case is when you want to set your own logger. Sinatra will use whatever it will find in env['rack.logger'].


Rack::CommonLogger generates the log messages internally (I think).

Isaac Betesh

See my suggestion here: Logging in Sinatra?

However, it sounds from your question like the problem is that App is a module.

your Sinatra main class should be something like this:

class App < Sinatra::Base

as opposed to

module App < Sinatra::Base

Make sure you've defined it as a class and that error should go away.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!