Writing Ruby Console Output to Text File - ruby-on-rails

I'm trying to run a Ruby application, which outputs information to the console. I'd like to make this output get saved to a text/log file.
Is this even possible? There must be a flag for ruby to do this right?

Use shell redirections:
ruby script.rb > out.txt
rails server > out.txt
On another note, you may also redirect $stdout, $stderr:
$stdout = File.new('/path/to/out.txt', 'w')
http://eddymulyono.livejournal.com/65791.html

Related

Rails logger logging to console

I am using Rails logger to log information in my Rails 4 application.
I do not set config.logger=Logger.new(STDOUT) in my configuration and just keep the default option for logger.However, besides the log file under /log folder, the information is still printed to the console when the statement Rails.logger.info is used.
Why does this happen as I only want the log info to be inside the log file, not printed to console? Thanks
Thats how rails s behaves. It puts the logs in log/development.log as well as show at the command line. As thats more convenient for most cases.
Anyways if you do not want it you can run your server something like this:
rails s 1> /dev/null
This would redirect all that text to /dev/null
If you only wants to print the log in log/development.log and not in the console, then use below to start your rails server.
rails s > log/development.log

Is it possible to determine if ruby code block is executed under http server or as standalone script?

I am writing a ruby gem and I want to be able to differentiate if gem is being used under http server (i.e invoked as a part of rails framework) or If ruby gem is is invoked from standalone script say like ruby test_script.rb.
I know I could check certain ENV variables such as 'REMOTE_ADDR' , 'HTTP_REFERER' to decide if it originated from web application or standalone script. But obviously it wont be correct all the time so was just wondering if there is any better way to check if ruby code block is being executed under http server.
thanks
You can check if a ruby script is being run on the command line (or not) by checking if the current file name matches the name of the file that is being executed.
if __FILE__ == $0
# ruby gem_name.rb
puts "On the command line"
else
# require 'gem_name'
puts "Gem has been required"
end

How to set up a file that I can run through my command line?

I am using Sublime Text and Ruby on Rails. Everything is installed.
How do I set up a file like random.rb
that I can navigate to in my git bash and run its contents.
The main reason I want to do this is to play with classes, methods and functions and see their outputs. IRB is not enough for when I want to build entire functions, etc.
How would I go about achieving this?
First example: simply run your Ruby code from the IRB prompt:
$ irb
> puts 'playing in irb prompt'
Second example: write your Ruby code to a ".rb" file and run it by using ruby file_name.rb:
#test.rb
puts 'Hello World'
$ ruby test.rb
-> "Hello world"
Third example: from Rails' console, you can run your .rb file:
$ rails console
$ eval(File.read 'test.rb')
-> 'Hello world'
#make sure your test.rb file resides in your project root directory.
Fourth example: use rails console to interact with your application model:
$ rails console
$ User.all #depending on what model you have.

.bashrc equivalent for rails console?

When I'm using the rails console I like having a clear! command along side the reload! command, so every time I launch the rails console I write
def clear!
system('clear')
end
When I repeat behavior in my bash shell I add it to my ~/.bashrc file. Is there a similar way for me to do this for my rails console?
Create a file in your home directory named ~/.irbrc. Inside, define any functions or settings you want to be applied to your irb.
Here's an example that explains what I mean.
You can do this with Pry if you use that instead of irb. You can configure custom commands in a ~/.pryrc
Pry.config.commands.command "clear!", "Clears the display" do |*args|
system("clear")
end
See pry-rails

Can I get Rails debugging output in Pow similar to WEBrick?

When I use rails/server (WEBrick) I get constant debug info (queries, etc) from my rails app as console output. Is there a way to get this debug output with Pow?
Thanks
You can check the HTTP requests that Pow receives by running tail -f on the log file of your choice in the ~/Library/Logs/Pow directory. Check out the Pow manual section on Viewing Log Files
If you're looking for Rails specific logs, they'll be located in the log/ directory of your application. For instance, if you want to watch the development log for your application switch to the root director of the application and run:
tail -f log/development.log
There's also less which will give you a few more options, but isn't quite as simple to use.
less -R log/development.log
and then press ctrl-f to follow new output to the file. Pressing h will give you a more detailed help menu.
You can also use a helper gem like powder. I order to show application log just type powder applog
Do you see a log/development.log path in your application? You can typically use that to see what is taking place within Rails. There are also logs for testing and production as well and they might be present for your project depending on the mode that the application is running in.
To access these logs you should use the Terminal and cd to your application, you can then use a utility such as tail to see the logs. A variation of the tail command would also scroll the output when there is new content like the Rails logger normally does.
Use the pry-remote gem.
References:
https://github.com/Mon-Ouie/pry-remote
https://coderwall.com/p/sreazq

Resources