开发者

rails - Redirecting console output to a file

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开发者_如何学Python 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.


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


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


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


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.


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜