rails - Redirecting console output to a file

60,447

Solution 1

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 2

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 3

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

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

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
Share:
60,447
kikito
Author by

kikito

scripted: ruby on rails, javascript, lua compiled: C++, Java Opensource when possible, thanks.

Updated on March 07, 2020

Comments

  • kikito
    kikito about 4 years

    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.

  • kikito
    kikito about 14 years
    This is helpful, but the other answer does what I wanted in a simpler way. Thanks for taking the time to answer anyway.
  • kikito
    kikito about 14 years
    Thank you! This is exactly what I was looking for.
  • kikito
    kikito about 14 years
    Thanks for answering, but I wasn't looking for pagination; I wanted to see everything on a single page.
  • Peter DeWeese
    Peter DeWeese about 12 years
    It didn't work for me until I added $stdout.sync=true. Edited.
  • Hued Bafuli
    Hued Bafuli over 11 years
    $stdout.reopen("my.log", "w") seems to be a more elegant solution, seen at: stackoverflow.com/a/2480439/21217
  • Magne
    Magne over 9 years
    It gave me NoMethodError: undefined method 'statements' for #<Blog:0x007fdaadbe4530> or undefined method 'statements' for #<ActiveRecord::Relation:0x007fdaaaf4f880> :(
  • sbonami
    sbonami almost 9 years
    Thanks for this, exactly what I was looking for!
  • SujitS
    SujitS almost 9 years
    Where is the file statements.xml get saved in the system. I cannot find the file.
  • B. Shea
    B. Shea about 8 years
    This is the better answer to show how to save specific record dump rather than telling someone to add yet another gem or plugin (SO tired of software recommendations when not needed) OR dumping everything in console to file. Voted up.
  • dhrubo_moy
    dhrubo_moy over 7 years
    $stdout = File.new('console.out', 'w') taking really long time. I am using windows. Not sure if that is the reason!
  • kapad
    kapad almost 6 years
    this didn't work for me.. I tried running rails server | tee file.txt and there's no output to STDOUT or the file. I just think the server fails to start in this case.
  • Kevin
    Kevin about 5 years
    server prints to stderr I think, there should be options in the tee command to redirect that as well.
  • labyrinth
    labyrinth about 5 years
    Yes, do something like rails console 2>&1 | tee file.txt. Not an option in tee, just simple redirection of stderr to stdout so both end up in your file.txt.
  • Matt
    Matt about 4 years
    The file ended up full of console colour codes. Use pp to get output without colour codes.
  • user664833
    user664833 about 4 years
    @SujitS - The file gets saved into the directory from which you executed rails console (most likely your app's root directory). (At least this was the case for me.)
  • Moshe Yudkowsky
    Moshe Yudkowsky over 2 years
    You may need to close the file to capture all the data. I use std=$stdout ; f=File.new("/tmp/console.out", 'w') followed by $stdout=f ; pp Foo.where(id: [100..1234]); f.close; $stdout=std. Otherwise final pieces of data do not appear.
  • frank_108
    frank_108 over 2 years
    Variation on the solution is: f = File.new('/tmp/statements.xml', 'w') then f.puts Account.find(1) (or whatever is the query). Finally, closing the file: f.close. This worked properly when doing a query in gitlab-rails console.