Rails- 将控制台输出重定向到文件

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

cd mydir
ls -l > mydir.txt

> 操作符捕获标准输入并将其重定向到一个文件; 因此我在 mydir.txt中获得文件列表,而不是在标准输出中。

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

我有一个 ruby 语句,可以生成大量的输出(大约8k 行) ,我希望能够完全看到它,但是控制台只能“记住”最后1024行左右。所以我想重定向到一个文件-如果有人知道更好的选择,我洗耳恭听。

64874 次浏览

You can use override $stdout to redirect the console output:

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

You may also need to call this once:

$stdout.sync = true

To restore:

$stdout = STDOUT

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

if "irb" == $0
config.logger = Logger.new(Rails.root.join('path_to_log_file.txt'))
end

You can also rotate the log file using

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

For logging only active record related operations, you can do

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

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

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:

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

For more on how this works, read this post.

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:

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

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.

From the Hirb readme:

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

# 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

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

Just open your rails project directory and enter the command:

rails c | tee output.txt

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

man tee

Try using script utility if you are on Unix-based OS.

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.

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 script and then ran the rails runner in script session but it didn't wrote everything. Then I found this script -c "runner command here" output_file and it saved all the output as was desired. This was on Ubuntu 14.04 LTS

References:

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