Sinatra中的简单理想日志记录 [英] Simple and Ideal Logging in Sinatra

查看:72
本文介绍了Sinatra中的简单理想日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了一些博客和网站,这些博客和网站为我提供了有关如何登录sinatra的一些信息,但不适用于我的应用程序,而且我还浏览了一个名为

I went through few blogs and sites which gave me some information about how to log in sinatra but didnt work for my app and also i went through a gem called sinatra-logger didnt tried it, wanted to know ideal and simple way to "log" in sinatra. Like we do logger.info for rails.

我尝试过但没有工作的代码来自以下网站

The code which i tried and didnt work is from the following site link and also some of SO links were too pointing to the same approach used in the link above.

configure do
 LOGGER = Logger.new("sinatra.log")
end

helpers do
 def logger
  LOGGER
 end
end

我已经在我的应用程序的app.rb中编写了此代码,但它失败了为App:Module定义了未定义的方法"configure"

i have written this in app.rb of my app, it fails saying undefined method `configure' for App:Module

任何指针或指南都将有所帮助.谢谢.

Any pointers or guide would be helpful. Thanks.

编辑 现在,我正在使用文件写入功能来记录我的错误和消息:

Edits Right Now am using file write to log my errors and messages:

File.open('log.txt', 'a') do |f|
 f.write "#{status}-#{CONFIG.api_url}-#{data.inspect}-tweet}"
end

推荐答案

如果您使用的是Sinatra 1.3,则应该能够像使用logger.info

If you are using Sinatra 1.3 you should be able to log just like in rails with logger.info

以下内容摘录自 Sinatra自述文件:

在请求范围内,记录器助手公开一个Logger实例:

In the request scope, the logger helper exposes a Logger instance:

get '/' do
  logger.info "loading data"
  # ...
end

此记录器将自动考虑您的Rack处理程序的记录设置.如果禁用了日志记录,则此方法将返回一个虚拟对象,因此您不必担心路由和过滤器.

This logger will automatically take your Rack handler’s logging settings into account. If logging is disabled, this method will return a dummy object, so you do not have to worry in your routes and filters about it.

请注意,默认情况下仅对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

为避免设置任何日志记录中间件,请将日志记录设置设置为nil.但是,请记住,在这种情况下,记录器将返回nil.一个常见的用例是您要设置自己的记录器. Sinatra将使用它将在env ['rack.logger']中找到的任何内容.

To avoid any logging middleware to be set up, set the logging setting to nil. However, keep in mind that logger will in that case return nil. A common use case is when you want to set your own logger. Sinatra will use whatever it will find in env['rack.logger'].

Rack :: CommonLogger 在内部生成日志消息(思考).

Rack::CommonLogger generates the log messages internally (I think).

这篇关于Sinatra中的简单理想日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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