简体   繁体   English

如何使SInatra和Rack输出到Logger实例?

[英]How do I get SInatra and Rack to output to my Logger instance?

I have a Sinatra application which outputs a steady stream of information to STDOUT via an instance of Logger. 我有一个Sinatra应用程序,它通过Logger实例将稳定的信息流输出到STDOUT。 I have also turned on Sinatra logging, and Rack logging. 我还打开了Sinatra日志记录和Rack日志记录。 For argument's sake lets say that this is a good idea. 为了论证,可以说这是一个好主意。

Here's what the output looks like at the moment: 这是当前输出的样子:

[2016-03-03 11:32:38] INFO  WEBrick 1.3.1
[2016-03-03 11:32:38] INFO  ruby 2.3.0 (2015-12-25) [x86_64-linux]
[2016-03-03 11:32:38] INFO  WEBrick::HTTPServer#start: pid=12303 port=4567
[03/03/16 11:32:45] core/lib/registry.rb     INFO Resolving request for u:/swingshift/registry a:GET
[03/03/16 11:32:45] core/lib/registry.rb     INFO SwingShift::RegistryController has been selected...
10.192.0.136 - - [03/Mar/2016:11:32:45 +0000] "GET /swingshift/registry HTTP/1.1" 200 10227 0.0557
10.192.0.136 - - [03/Mar/2016:11:32:46 +0000] "GET /css/bootstrap.min.css HTTP/1.1" 304 - 0.0023

As you can see, it's a bit of a mess. 如您所见,有点混乱。 What I would like is to tell Sinatra and Rack to use my instance of Logger, rather than their own. 我想告诉Sinatra和Rack使用我的Logger实例,而不是他们自己的实例。

  • How do I get Sinatra to use my logger? 我如何让Sinatra使用记录仪?
  • How do I get Rack to use my logger? 如何让Rack使用记录仪?

(Alternatively, can I pass a proc to their loggers to format them the same? That's not as good, but it would be something.) (或者,我可以将proc传递给他们的记录器以对它们进行格式化吗?虽然不尽如人意,但是会有所帮助。)

Here's the my Sinatra settings block. 这是我的Sinatra设置块。 At this point $logger already points to my Logger instance: 至此,$ logger已经指向我的Logger实例:

configure do                                                
  $logger.info(__FILE__){"Starting..."}                     
  set :bind, '0.0.0.0'                                      
  enable :sessions                                          
  set :session_secret, 'whatever'

  set :views, BASEDIR
  set :haml, :layout_options => { :views => 'core/views' }  

  enable :logging                                           
  set :dump_errors, true                                    
end                     

I've also got a 我也有一个

use Rack::CommonLogger, $logger

...but it doesn't seem to do anything. ...但是它似乎无能为力。

Update:: I'm aware of https://stackoverflow.com/questions/2239240A/use-rackcommonlogger-in-sinatra , but: 更新::我知道https://stackoverflow.com/questions/2239240A/use-rackcommonlogger-in-sinatra ,但是:

  • It doesn't address Sinatra logging, just Rack 它不涉及Sinatra日志记录,仅涉及Rack
  • The accepted answer seems extremely complex and there are a number of unanswered points raised in the comments to it. 可接受的答案似乎非常复杂,并且在注释中提出了许多未解决的问题。 (Do I really need to set a logger in config.ru AND add a middleware logging layer? Is the syntax for the logging layer wrong, as >=4 people seem to think?) (我是否真的需要在config.ru中设置记录器并添加中间件日志记录层?日志记录层的语法是否错误,因为> = 4的人似乎在思考?)

According to Sinatra I'm supposed to be able to control Rack from its settings block. 根据Sinatra的说法,我应该能够从其settings模块控制Rack。 Is this not true? 这不是真的吗

Update 2: I've now had a proper go at the solution to the other question. 更新2:我现在可以适当解决其他问题了。 Implementing it as is does ... absolutely nothing to the log here. 照原样实施它……这里的日志绝对没有。

I'm not using WEBrick but Thin ( set :server, 'thin' ) Anyway according to Sinatra configuration I redirect rack.errors to my $logger : 我没有使用WEBrick,而是使用Thin( set :server, 'thin' ),无论如何根据Sinatra的配置,我将rack.errors重定向到$logger

class IOToLog < IO
  def initialize(logger)
    @logger = logger
  end
  def write(string)
    @logger.debug {"SINATRA #{string.strip}"}
  end
end
IOTOLOG = IOToLog.new($logger)

before {
  env["rack.errors"] =  IOTOLOG
}

and the output is: 输出是:

2017-06-21 15:44:46.166 DEBUG SINATRA 127.0.0.1 - - [21/Jun/2017:15:44:46 +0200] "GET /wd/hub/status HTTP/1.1" 200 83 0.0004
2017-06-21 15:44:51.167 DEBUG SINATRA 127.0.0.1 - - [21/Jun/2017:15:44:51 +0200] "GET /wd/hub/status HTTP/1.1" 200 83 0.0004

BTW I've tried env["rack.logger"] = $logger but with no luck. 顺便说一句,我已经尝试过env["rack.logger"] = $logger但是没有运气。

I can't comment yet so posting answer. 我还不能发表评论,所以发布答案。 You can format the messages going to IOToLog. 您可以格式化进入IOToLog的消息。 You need to set the format in before block. 您需要在before块中设置格式。 Maybe something like 也许是这样的

before {
   env["rack.errors"] =  IOTOLOG
   IOTOLOG.format
}

And in IOToLog add something like: 并在IOToLog中添加如下内容:

class IOToLog < IO
    def format
      @logger.formatter = proc do |severity, datetime, progname, msg|
      "#{severity}: #{msg}\n"
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得正在通过机架测试测试的Sinatra应用程序实例? - How do I get the Sinatra app instance that's being tested by rack-test? 使用 Sinatra 和 Rack Logger 登录时如何过滤敏感信息 - How to filter sensitive information when logging with Sinatra and Rack Logger 如何获得Rails记录我的Rack :: CommonLogger消息? - How do I get Rails to log my Rack::CommonLogger messages? 如何在请求范围之外访问Sinatra的Logger - How do I access Sinatra's Logger outside the Request Scope 如何获取Sinatra应用程序的代码覆盖率统计信息? - How do I get code coverage stats for my Sinatra app? 如何从sinatra应用程序中的类将数据传递到websocket机架? - How can I pass data to websocket-rack from a class in my sinatra app? 如何测试带有Memcache的Heroku上的Rack :: Cache是​​否可与Sinatra应用一起使用? - How do I test if Rack::Cache is functioning with Sinatra app on Heroku with memcache? 如何使用机架/测试来测试Sinatra重定向? 该应用程序有效,但测试不起作用 - How do I use Rack/Test to test Sinatra redirects? The App works, but the test doesn't 如何让Sinatra使用HTTPClient? - How do I get Sinatra to work with HTTPClient? 如何查看Sinatra服务器在执行“ Rack :: Protection”操作? - How can I see what `Rack::Protection` is doing with Sinatra server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM