将IO.popen的输出重定向到日志文件中

时间:2012-08-30 08:17:14

标签: ruby wkhtmltopdf pdfkit

我正在运行PDFKit Gem以使用wkhtmltopdf生成pdfs。

在我的项目上运行规范或生成PDF时,wkhtmtopdf会将其输出泄漏到stdout /不同的日志,但我希望在我的应用程序中将所有输出生成在单独的日志文件pdfkit.log中。

wkhtmltopdf的调用很简单

result = IO.popen(invoke, "wb+") do |pdf|
  pdf.puts(@source.to_s) if @source.html?
  pdf.close_write
  pdf.gets(nil)
end

我尝试according to the IO.popen > Kernel.spawn documentation重新配置IO.popen调用以将所有输出放入日志文件中:err => [:child, :out]应该合并stderrstdout,并且:out=>[File.join(Rails.root, 'log', 'pdfkit.log'), "w"]应写入指定的日志文件)

result = IO.popen(invoke, "wb+", :out=>[File.join(Rails.root, 'log', 'pdfkit.log'), "w"], :err=>[:child, :out]) do |pdf|
  pdf.puts(@source.to_s) if @source.html?
  pdf.close_write
  pdf.gets(nil)
end

该方法的覆盖已经到位,但遗憾的是无法按预期工作。我知道错误是我误解了IO.popen的选项,但我不知道如何......

2 个答案:

答案 0 :(得分:0)

$stdout.reopen("out.txt", "w")
$stderr.reopen("err.txt", "w")

答案 1 :(得分:0)

也许尝试这样的事情:

in_io_pipe, out_io_pipe = IO.pipe
out = File.new(File.join(Rails.root, 'log', 'pdfkit.log'), 'w+')
pid = Process.spawn(cmd, :out => out, :in => in_io_pipe, :err => out)
Process.detach(pid)
out_io_pipe.puts("something")
相关问题