rails - Redirecting console output to a file

前端 未结 7 827
时光取名叫无心
时光取名叫无心 2020-12-12 09:51

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

相关标签:
7条回答
  • 2020-12-12 10:25

    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
    
    0 讨论(0)
  • 2020-12-12 10:26

    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
    

    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 $stdout so you can change it back.

    0 讨论(0)
  • 2020-12-12 10:27

    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
    
    0 讨论(0)
  • 2020-12-12 10:28

    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
    
    0 讨论(0)
  • 2020-12-12 10:29

    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.

    0 讨论(0)
  • 2020-12-12 10:33

    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

    0 讨论(0)
提交回复
热议问题