To simulate a production setup, for example, allow multiple request processes running simultaneously, we want to serve content using Unicorn in development.

If you are using the thin server, it already has a built-in command: rails server thin.

Here’s a quick guide of running Unicorn instead of default Rails WEBrick server under development mode. You will be able to execute rake server to start a development server with standard Rails development log output in STDOUT.

Gemfile

First, make sure that unicorn is included in Rails Gemfile, also make sure running bundle update to update gem dependencies.

Create a Rake Task

Create a new rake task file under Rails directory lib/tasks with a sensible name, for example, dev_server.rake. Unicorn ships unicorn command for Rails specifically, it’s simple to write a server task:

Refine Terminal Logs

Starting the server by running rake server or simply rake s at this stage, you will notice that the terminal only contains unicorn logs, request logs are missing. That is because Rails writes log to a file instead of STDOUT by default. Adding the following lines in config/environments/development.rb to produce request logs in terminal:

Multiple IO Logs

Rails only support assigning one logger in the configuration. If the desire is to log Unicorn and Rails log to both STDOUT and file, a custom multiple stream writer is required. Here’s how you can achieve it. The following example shows an improved development.rb configuration working with a custom MultipleIO writer class.

References

Leave a comment