Sinatra在日志文件中未显示异常 [英] Sinatra doesn't show exceptions in log file

查看:58
本文介绍了Sinatra在日志文件中未显示异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Rails到sinatra,使用日志记录时遇到了一些问题.我有一个Sinatra应用程序,它的记录方式如下:

I'm coming from Rails to sinatra, and I have some problems using logging. I have a Sinatra app, that does it's logging like so:

  configure do
    Logger.class_eval { alias :write :'<<' }
    logger = Logger.new("log/#{settings.environment}.log")
    use Rack::CommonLogger, logger
  end

所有请求均已正确记录,我看到某事

All requests are logged properly, I see sth like

127.0.0.1 - - [25/May/2013 10:34:21] "GET / HTTP/1.1" 200 30 0.0021
127.0.0.1 - - [25/May/2013 10:34:22] "GET /favicon.ico HTTP/1.1" 404 18 0.0041

在日志文件中.但是我也想将应用程序错误记录到日志文件中.当我使用RACK_ENV=production rackup config.ru在生产环境中启动我的应用程序时,发生错误,它仅记录500 http状态,而不记录错误本身.该错误显示在控制台内部.这对于开发来说是很好的,但对于生产而言则不是.错误应出现在日志文件中,以供以后调试.

Inside the log files. But I also want to log application errors to the logfile. When I start my app in production env with RACK_ENV=production rackup config.ru and I an error occurs, it only logs a 500 http status, but not the error itself. The error is shown inside the console. This is fine for development, but not for production. The errors should appear inside the log file for later debugging.

这是我的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

我在configure do块中玩过enable :logging, :dump_errors, :raise_errors,但这没什么.是因为我将sinatra用作模块化应用程序吗?在get "/"路由内,我可以访问configure块内设置的变量.

I've played around with the enable :logging, :dump_errors, :raise_errors inside the configure do block, but that doesn't anything. Is it, because I'm using sinatra as a modular application? Inside the get "/" route I can access the variables set inside the configure block.

那么,使用sinatra将错误记录到文件中的最佳方法是什么?

So any idea, what the best practise is, to log errors to a file with sinatra?

推荐答案

在此处阅读文档: http ://www.sinatrarb.com/intro.html#Logging

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

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

这篇关于Sinatra在日志文件中未显示异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆