登录 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 将附带这样一个记录器对象,完全可用如上.您可以按照The Bleeding Edge"中所述使用边缘 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屋!