[英]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实例,而不是他们自己的实例。
(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 ,但是:
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.