2

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

在我的项目上运行规范或生成 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

根据 IO.popen > Kernel.spawn 文档,我尝试重新配置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,但我不明白如何..

4

2 回答 2

0
$stdout.reopen("out.txt", "w")
$stderr.reopen("err.txt", "w")
于 2012-08-30T10:02:10.143 回答
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")
于 2017-04-05T21:47:07.003 回答