我写过以下webrick服务器。它成功写入2个日志文件。
我想将r10kstatus命令的输出写入另一个日志文件,但我有点过头了。
#!/usr/bin/env ruby
require 'webrick'
server = WEBrick::HTTPServer.new(
:Port => ARGV.first,
:Logger => WEBrick::Log.new("webrick.log",WEBrick::Log::INFO),
:AccessLog => [[File.open("webrick_access.log",'w'),WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
)
server.mount_proc '/' do |req, res|
r10kstatus = `sudo r10k deploy environment -pv 2>&1`
log_foo = WEBrick::Log.new("r10k_deploy.log",WEBrick::Log::DEBUG)
File.write(log_foo, r10kstatus)
end
trap 'INT' do
server.shutdown
end
server.start
http://www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/BasicLog.html#method-i-log
这3行不起作用
r10kstatus = `sudo r10k deploy environment -pv 2>&1`
log_foo = WEBrick::Log.new("r10k_deploy.log",WEBrick::Log::DEBUG)
File.write(log_foo, r10kstatus)
如何将r10kstatus
的stdout写入日志文件?
答案 0 :(得分:1)
想出来。
Public Class方法和Public实例方法之间存在差异。
我必须创建一个对象,然后针对该对象调用debug实例方法
完整解决方案:
#!/usr/bin/env ruby
require 'webrick'
server = WEBrick::HTTPServer.new(
:Port => ARGV.first,
:Logger => WEBrick::Log.new("r10k_gitlab_webhook.log",WEBrick::Log::INFO),
:AccessLog => [[File.open("r10k_gitlab_webhook.log",'w'),WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
)
r10k_comman_log = WEBrick::Log.new('foo.log',WEBrick::Log::DEBUG)
r10k_comman_log.warn( 'hi' )
server.mount_proc '/' do |req, res|
r10kstatus = `whoami`
r10k_comman_log.debug( r10kstatus )
end
trap 'INT' do
server.shutdown
end
server.start