- Heroku Cookbook
- Mike Coutermarsh
- 476字
- 2021-08-05 17:14:28
Searching logs
Heroku does not have the built-in capability to search our logs from the command line. We can get around this limitation easily by making use of some other command-line tools.
In this recipe, we will learn how to combine Heroku's logs with Grep, a command-line tool to search text. This will allow us to search our recent logs for keywords, helping us track down errors more quickly.
Getting ready
For this recipe, we'll need to have Grep installed. For OS X and Linux machines, it should already be installed. We can install Grep using the following steps:
- To check if we have Grep installed, let's open up a terminal and type the following:
$ grep usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file] [--binary-files=value] [--color=when] [--context[=num]] [--directories=action] [--label] [--line-buffered] [--null] [pattern] [file ...]
- If we do not see usage instructions, we can visit http://www.gnu.org/software/grep/ for the download and installation instructions.
How to do it…
Let's start searching our logs by opening a terminal and navigating to one of our Heroku applications using the following steps:
- To search for a keyword in our logs, we need to pipe our logs into Grep. This simply means that we will be passing our logs into Grep and having Grep search them for us. Let's try this now. The following command will search the output of
heroku logs
for the worderror
:$ heroku logs | grep error
- Sometimes, we might want to search for a longer string that includes special characters. We can do this by surrounding it with quotes:
$ heroku logs | grep "path=/pages/about host"
- It can be useful to also see the lines surrounding the line that matched our search. We can do this as well. The next command will show us the line that contains an error as well as the three lines above and below it:
$ heroku logs | grep error -C 3
- We can even search with regular expressions. The next command will show us every line that matches a number that ends with MB. So, for example, lines with 100 MB, 25 MB, or 3 MB will all appear:
$ heroku logs | grep '\d*MB'
Note
To learn more about regular expressions, visit http://regex.learncodethehardway.org/.
How it works…
Like most Unix-based tools, Grep was built to accomplish a single task and to do it well. Global regular expression print (Grep) is built to search a set of files for a pattern and then print all of the matches.
Grep can also search anything it receives through standard input; this is exactly how we used it in this recipe. By piping the output of our Heroku logs into Grep, we are passing our logs to Grep as standard input.
See also
- To learn more about Grep, visit http://www.tutorialspoint.com/unix_commands/grep.htm
- 垃圾回收的算法與實現
- Flink SQL與DataStream入門、進階與實戰
- AIRAndroid應用開發實戰
- Learning Elixir
- Android 應用案例開發大全(第3版)
- Scala程序員面試算法寶典
- Python數據結構與算法(視頻教學版)
- Instant Lucene.NET
- Microsoft Azure Storage Essentials
- ExtJS Web應用程序開發指南第2版
- Managing Microsoft Hybrid Clouds
- PHP動態網站開發實踐教程
- SQL Server 2014 Development Essentials
- PHP高性能開發:基礎、框架與項目實戰
- 亮劍C#項目開發案例導航