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
Web server logs usually live in
Searching through single logs with
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
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/your-project.de/shared/log && zgrep "keyword" *
Note that Capistrano 3 no longer supports the
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
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.