spuder
spuder

Reputation: 18427

Write to file with WEBrick log

I've written the following webrick server. It succesfully writes 2 log files.

I want to write the output of the r10kstatus command to a different log file, but I'm in a bit over my head.

#!/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

These 3 lines are not working

  r10kstatus = `sudo r10k deploy environment -pv 2>&1`
  log_foo = WEBrick::Log.new("r10k_deploy.log",WEBrick::Log::DEBUG)
  File.write(log_foo, r10kstatus)

How could I write the stdout of r10kstatus to a log file?

Upvotes: 0

Views: 2265

Answers (1)

spuder
spuder

Reputation: 18427

Figured it out.

There is a difference between Public Class methods and Public instance methods.

I had to create an object, and then call the debug instance method against that object

Full solution:

#!/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

Upvotes: 1

Related Questions