Posted over 2 years ago. Visible to the public. Repeats.

How to search through logs on staging or production environments

We generally use multiple application servers (at least two) and you have to search on all of them if you don't know which one handled the request you are looking for.

Rails application logs usually live in /var/www/<project-environment-name>/shared/log.
Web server logs usually live in /var/www/<project-environment-name>/log.

Searching through single logs with grep / zgrep

You can use grep in this directory to only search the latest logs or zgrep to also search older (already zipped) logs. zgrep is used just like grep and options will be passed to grep. For example

zgrep -B 2 "keyword" *

gives you two lines of context before every match for "keyword"

Running commands on multiple servers at the same time

There are multiple ways to run commands on multiple servers. They depend on your setup though.

With Capistrano 2

Capistrano 2 can run commands on multiple servers:
In your terminal (at the project directory), first open a capistrano shell (<env> will most of the time be staging or production):

cap <env> shell

Then run your commands. Remember, every command is executed by itself, so you have to chain multiple commands to run them together:

cd /var/www/ && zgrep "keyword" *

Note that Capistrano 3 no longer supports the shell command.

With Terminator

Use multiple shells in Terminator to ssh into each appliaction server. Then use the grouping feature of Terminator to send all keystrokes to all shells simultaneously. No need to chain the commands, just cd into the logs directory and then use grep / zgrep like in the example above.

With custom rake task

You can add a rake task which tails logs from all remote servers. See this card.

With a service

If you need to do this often you may want to set up some services to collect your logs, like ELK (Elasticsearch, Logstash & Kibana), instead of using one of the solutions above.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Judith Roth
Last edit:
27 days ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Judith Roth to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more