登录Sinatra? [英] Logging in Sinatra?
问题描述
我在弄清楚如何使用Sinatra记录消息时遇到了麻烦.我不是要记录请求,而是要在应用程序中的某些点记录自定义消息.例如,在获取URL时,我想登录"Fetching #{url}"
.
I'm having trouble figuring out how to log messages with Sinatra. I'm not looking to log requests, but rather custom messages at certain points in my app. For example, when fetching a URL I would like to log "Fetching #{url}"
.
这就是我想要的:
- 指定日志级别的功能(例如:
logger.info("Fetching #{url}")
) - 在开发和测试环境中,消息将被写入控制台.
- 在生产中,仅写出与当前日志级别匹配的消息.
我猜想这可以在config.ru
中轻松完成,但是我不确定100%是否要启用哪个设置,以及是否必须自己手动创建Logger
对象(还有, Logger
使用的类别:Logger
,Rack::Logger
或Rack::CommonLogger
).
I'm guessing this can easily be done in config.ru
, but I'm not 100% sure which setting I want to enable, and if I have to manually create a Logger
object myself (and furthermore, which class of Logger
to use: Logger
, Rack::Logger
, or Rack::CommonLogger
).
(我知道在StackOverflow上也有类似的问题,但是似乎没有一个问题可以直接回答我的问题.如果您能指出我现有的问题,我会将其标记为重复).
(I know there are similar questions on StackOverflow, but none seem to directly answer my question. If you can point me to an existing question, I will mark this one as a duplicate).
推荐答案
Sinatra 1.3附带了这样的记录器对象,与上面所述完全一样.您可以按照"出血边缘中的说明使用边缘Sinatra.我猜我们要等到1.3发行才这么久.
Sinatra 1.3 will ship with such a logger object, exactly usable as above. You can use edge Sinatra as described in "The Bleeding Edge". Won't be that long until we'll release 1.3, I guess.
要在Sinatra 1.2中使用它,请执行以下操作:
To use it with Sinatra 1.2, do something like this:
require 'sinatra'
use Rack::Logger
helpers do
def logger
request.logger
end
end
这篇关于登录Sinatra?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!