1. Aug 23rd, 2007

    One eye on the browser, one eye on the console

    Step 1. Drop this in lib/tasks/development.rake:

    task "server" do
      # Dump lines from the file to the console at the specified interval (in seconds).
      def tail(file_name, interval = 1)
        position = File.size(file_name)
        leftover = ""
        while true
          sleep interval
          File.open file_name do |file|
            file.seek position
            text = file.read
            lines = text[/([^n]*n)*/m]
            if lines.empty?
              leftover << text
            else
              puts (leftover + lines)
              leftover = text[lines.size..-1]
            end
            position = file.tell
          end
        end
      end
    
      pid = "#{RAILS_ROOT}/tmp/pids/mongrel.pid"
    
      trap "INT" do
        puts "Stopping mongrel"
        puts `mongrel_rails stop -P #{pid}`
        exit
      end
    
      puts "Starting mongrel, hit Ctrl-C to stop"
      puts `mongrel_rails start -d -e #{RAILS_ENV} -P #{pid}`
      tail "log/development.log"
    end

    Step 2. Start the server:

    rake start

    Step 3. Hit the browser, watch the log, hit Ctrl-C to kill.

    Update: If you’re using Sake (if not, what’s taking you so long?), you can skip step 1 and install it once for all your apps:

    sake -i http://pastie.caboo.se/90791.txt
    1. Aug 24th, 2007

      Chris

      Sweet! Would make a nice Sake task.

    2. Aug 24th, 2007

      Assaf

      Copy to pastie, sake -i, and life is good!

    3. Aug 26th, 2007

      Morning Brew #67

      [...] One eye on the browser, one eye on the console – dumping logs to the console. [...]

    4. Aug 27th, 2007

      Jack Danger

      You could also run this straight in bash:
      mongrel_rails start -d && tail -f log/development.log

      I’ve got that aliased as ’sslog’

      Thanks for showing us how to do tail in Rake though!

    5. Aug 29th, 2007

      Assaf

      It actually started from the same bash line, which I got tired of retyping, so I moved it into rake by making system calls, and then rewrote it to run on the UNIOS/X and that other GUI platform.

    6. Sep 9th, 2007

      Aníbal

      Not only we are using Sake, but we used the RailsRumble to build a directory where people can submit, tag ans search sake snippets. Please feel free to take a look at it: http://sakebar.railsrumble.com/

    Your comment, here ⇓