Ruby-on-rails rails - 将控制台输出重定向到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2461503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:24:31  来源:igfitidea点击:

rails - Redirecting console output to a file

ruby-on-railsrubyfileconsole

提问by kikito

On a bash console, if I do this:

在 bash 控制台上,如果我这样做:

cd mydir
ls -l > mydir.txt

The > operator captures the standard input and redirects it to a file; so I get the listing of files in mydir.txtinstead of in the standard output.

> 运算符捕获标准输入并将其重定向到文件;所以我得到了文件列表mydir.txt而不是标准输出。

Is there any way to do something similar on the rails console?

有什么办法可以在 rails 控制台上做类似的事情吗?

I've got a ruby statement that generates lots of prints (~8k lines) and I'd like to be able to see it completely, but the console only "remembers" the last 1024 lines or so. So I thought about redirecting to a file - If anyone knows a better option, I'm all ears.

我有一个 ruby​​ 语句,它生成大量打印(约 8k 行),我希望能够完整地看到它,但控制台只“记住”最后 1024 行左右。所以我想重定向到一个文件 - 如果有人知道更好的选择,我全神贯注。

回答by Minimul

A quick one-off solution:

一种快速的一次性解决方案:

irb:001> f = File.new('statements.xml', 'w')
irb:002> f << Account.find(1).statements.to_xml
irb:003> f.close

Create a JSON fixture:

创建一个 JSON 夹具:

irb:004> f = File.new(Rails.root + 'spec/fixtures/qbo/amy_cust.json', 'w')
irb:005> f << JSON.pretty_generate((q.get :customer, 1).as_json)
irb:006> f.close

回答by Veger

You can use override $stdoutto redirect the console output:

您可以使用 override$stdout重定向控制台输出:

$stdout = File.new('console.out', 'w')

You may also need to call this once:

您可能还需要调用一次:

$stdout.sync = true

This will redirect all output to the file. If you want to temporarily redirect the output make sure that you store the original value of $stdoutso you can change it back.

这会将所有输出重定向到文件。如果您想临时重定向输出,请确保存储 的原始值,$stdout以便您可以将其更改回来。

回答by Chandra Agarwala

Apart from Veger's answer, there is one of more way to do it which also provides many other additional options.

除了 Veger 的回答之外,还有一种方法可以做到这一点,它还提供了许多其他附加选项。

Just open your rails project directory and enter the command:

只需打开您的 rails 项目目录并输入命令:

rails c | tee output.txt

tee command also has many other options which you can check out by:

tee 命令还有许多其他选项,您可以通过以下方式查看:

man tee

回答by Chirantan

If you write the following code in your environment file, it should work.

如果您在环境文件中编写以下代码,它应该可以工作。

if "irb" == 
config.logger = Logger.new(Rails.root.join('path_to_log_file.txt'), number_of_files, file_roation_size_threshold)
config.logger = Logger.new(Rails.root.join('path_to_log_file.txt')) end

You can also rotate the log file using

您还可以使用旋转日志文件

ActiveRecord::Base.logger = Logger.new(Rails.root.join('path_to_log_file.txt'))

For logging only active record related operations, you can do

对于仅记录活动记录相关操作,您可以执行

# Setup views to write to file 'console.log'.
>> Hirb::View.render_method = lambda {|output| File.open("console.log", 'w') {|f| f.write(output) } }

# Doesn't write to file because Symbol doesn't have a view and thus defaults to irb's echo mode.
>> :blah
=> :blah

# Go back to printing Hirb views to STDOUT.
>> Hirb::View.reset_render_method

This also lets you have different logger config/file for different environments.

这也使您可以为不同的环境使用不同的记录器配置/文件。

回答by Magne

Using Hirb, you can choose to log only the Hirb output to a text file. That makes you able to still see the commands you type in into the console window, and just the model output will go to the file.

使用 Hirb,您可以选择仅将 Hirb 输出记录到文本文件中。这使您仍然能够看到您在控制台窗口中输入的命令,并且只有模型输出将转到文件。

From the Hirb readme:

来自Hirb 自述文件

Although views by default are printed to STDOUT, they can be easily modified to write anywhere:

尽管默认情况下视图打印到 STDOUT,但可以轻松修改它们以写入任何地方:

>> require 'rubygems'
>> require 'hirb'
>> Hirb.enable

回答by cldwalker

Use hirb. It automatically pages any output in irb that is longer than a screenful. Put this in a console session to see this work:

使用hirb。它会自动对 irb 中比一屏长的任何输出进行分页。将其放在控制台会话中以查看此工作:

##代码##

For more on how this works, read this post.

有关其工作原理的更多信息,请阅读这篇文章

回答by Jignesh Gohel

Try using scriptutility if you are on Unix-based OS.

script如果您使用的是基于 Unix 的操作系统,请尝试使用实用程序。

script -c "rails runner -e development lib/scripts/my_script.rb" report.txt

script -c "rails runner -e development lib/scripts/my_script.rb" report.txt

That helped me capture a Rails runner script's very-very long output easily to a file.

这帮助我轻松地将 Rails runner 脚本的非常长的输出捕获到文件中。

I tried using redirecting to a file but it got written only at the end of script.

我尝试使用重定向到文件,但它仅在脚本末尾写入。

That didn't helped me because I had few interactive commands in my script.

这对我没有帮助,因为我的脚本中几乎没有交互式命令。

Then I used just scriptand then ran the rails runnerin script session but it didn't wrote everything. Then I found this script -c "runner command here" output_fileand it saved all the output as was desired. This was on Ubuntu 14.04 LTS

然后我使用 justscript然后运行rails runner脚本会话,但它并没有写下所有内容。然后我找到了这个script -c "runner command here" output_file,它根据需要保存了所有的输出。这是在 Ubuntu 14.04 LTS 上

References:

参考:

https://askubuntu.com/questions/290322/how-to-get-and-copy-a-too-long-output-completely-in-terminal#comment1668695_715798

https://askubuntu.com/questions/290322/how-to-get-and-copy-a-too-long-output-completely-in-terminal#comment1668695_715798

Writing Ruby Console Output to Text File

将 Ruby 控制台输出写入文本文件