rails - Redirecting console output to a file

Ruby on-RailsRubyFileConsole

Ruby on-Rails Problem Overview


On a bash console, if I do this:

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.txt instead of in the standard output.

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

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 on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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.

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

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

https://stackoverflow.com/questions/8720150/writing-ruby-console-output-to-text-file#comment86906526_8720196

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkikitoView Question on Stackoverflow
Solution 1 - Ruby on-RailsMinimulView Answer on Stackoverflow
Solution 2 - Ruby on-RailsVegerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsChandra AgarwalaView Answer on Stackoverflow
Solution 4 - Ruby on-RailsChirantanView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMagneView Answer on Stackoverflow
Solution 6 - Ruby on-RailscldwalkerView Answer on Stackoverflow
Solution 7 - Ruby on-RailsJignesh GohelView Answer on Stackoverflow