Logging 西纳特拉没有';t在日志文件中显示异常

Logging 西纳特拉没有';t在日志文件中显示异常,logging,sinatra,rack,middleware,Logging,Sinatra,Rack,Middleware,我从Rails来到sinatra,在使用日志时遇到了一些问题。我有一个Sinatra应用程序,就是这样记录的: configure do Logger.class_eval { alias :write :'<<' } logger = Logger.new("log/#{settings.environment}.log") use Rack::CommonLogger, logger end 在日志文件中。但我还想将应用程序错误记录到日志文件中。

我从Rails来到sinatra,在使用日志时遇到了一些问题。我有一个Sinatra应用程序,就是这样记录的:

  configure do
    Logger.class_eval { alias :write :'<<' }
    logger = Logger.new("log/#{settings.environment}.log")
    use Rack::CommonLogger, logger
  end
在日志文件中。但我还想将应用程序错误记录到日志文件中。当我使用
RACK_env=production rackup config.ru
在production env中启动我的应用程序时,出现错误,它只记录500 http状态,而不记录错误本身。该错误显示在控制台内部。这对开发是好的,但对生产不是好的。错误应该出现在日志文件中,以便以后调试

这是我的
config.ru

require 'rubygems'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
disable :run
Encoding.default_external = Encoding::UTF_8

use Rack::ShowExceptions
use Rack::Session::Pool

require File.expand_path '../app.rb', __FILE__
run App
这是我的
app.rb

class App < Sinatra::Base
  configure do
    set :public_folder, Proc.new { File.join(root, "public") }
    Logger.class_eval { alias :write :'<<' }
    logger = Logger.new("log/#{settings.environment}.log")
    use Rack::CommonLogger, logger
  end

  get '/' do
    raise "ERROR"
    erb :home, layout: :layout
  end
end
class-App
请注意,默认情况下仅为Sinatra::Application启用日志记录,因此,如果您从Sinatra::Base继承,您可能希望自己启用它:

class MyApp < Sinatra::Base
  configure :production, :development do
    enable :logging
  end
end
classmyapp
在此处阅读文档:

请注意,默认情况下仅为Sinatra::Application启用日志记录,因此,如果您从Sinatra::Base继承,您可能希望自己启用它:

class MyApp < Sinatra::Base
  configure :production, :development do
    enable :logging
  end
end
classmyapp
我让Sinatra将错误消息移动到文件的唯一方法是:

$stderr.reopen(<the file path>)
$stderr.reopen()
更详细的例子:

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
class-App
我让Sinatra将错误消息移动到文件的唯一方法是:

$stderr.reopen(<the file path>)
$stderr.reopen()
更详细的例子:

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
class-App
注意重新打开
标准输出将使乘客停止工作:/code>注意重新打开
标准输出将使乘客停止工作:/