Sinatra doesn't show exceptions in log file

天大地大妈咪最大 提交于 2019-12-06 11:52:53

Read the docs here: http://www.sinatrarb.com/intro.html#Logging

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

The only way I made Sinatra to move error messages to a file was:

$stderr.reopen(<the file path>)

More elaborated example:

class App < Sinatra::Base
  configure do
    set :logging, true
    set :root, File.dirname(__FILE__)
  end

  configure :development, :production do
    # console log to file
    log_path = "#{root}/log" 
    Dir.mkdir(log_path) unless File.exist?(log_path)
    log_file = File.new("#{log_path}/#{settings.environment}.log", "a+")
    log_file.sync = true
    $stdout.reopen(log_file)
    $stderr.reopen(log_file)
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!