【问题标题】:Proper logging setup for Sinatra + DataMapperSinatra + DataMapper 的正确日志设置
【发布时间】:2012-02-17 19:18:39
【问题描述】:

我想知道在 Sinatra 中以 DRY 方式设置配置块的正确方法是什么。我想要的是:

  • 在生产中,不要显示异常和错误
  • 在开发时,将查询记录到 DB
  • 在测试时,使用内存中的 SQLite db。

我设置如下:

configure :production do
  set :show_exceptions, false
  set :raise_errors, false
end

configure :development do
  DataMapper::Logger.new($stdout, :debug)
end

configure :test do
  DataMapper.setup(:default, "sqlite::memory:")
end

但是要在基本configuration 块中添加什么?这是一个正确的方法吗?另外,我在 Sinatra 中找不到配置块的正确执行顺序。

【问题讨论】:

    标签: ruby logging sinatra datamapper


    【解决方案1】:

    您不需要生产配置,因为这已经是默认设置。否则这看起来没问题。如果所有环境的设置都为真,则将其放在通用配置块中,如果它对一个或两个环境是特殊的,则将其设置为额外的块。有关所有详细信息,请参阅 Sinatra 自述文件。

    【讨论】:

      【解决方案2】:
      class App < Sinatra::Base
      
        configure :development do
          enable :logging, :dump_errors, :raise_errors
          disable :show_exceptions
          DataMapper::Logger.new(STDOUT, :debug, '[DataMapper] ')
          DataMapper::Model.raise_on_save_failure = true
        end
      
        configure :test do
          enable :dump_errors, :raise_errors
          disable :run, :logging, :show_exceptions
        end
      
        ## Log to file
        # FileUtils.mkdir_p 'log' unless File.exists?('log')
        # log_file = File.new('log/development.log', 'a')
      
        # $stdout.reopen(log_file)
        # $stderr.reopen(log_file)
        # $stderr.sync = true
        # $stdout.sync = true
      

      【讨论】:

        猜你喜欢
        • 2011-02-27
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2013-04-26
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多