- 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
- AngularJS Testing Cookbook
- TypeScript入門與實戰(zhàn)
- 編程卓越之道(卷3):軟件工程化
- Web交互界面設(shè)計與制作(微課版)
- 21天學(xué)通C++(第6版)
- Python 3破冰人工智能:從入門到實戰(zhàn)
- 精通MATLAB(第3版)
- Learning ArcGIS for Desktop
- 從零開始學(xué)Linux編程
- 詳解MATLAB圖形繪制技術(shù)
- Building Wireless Sensor Networks Using Arduino
- 軟件工程與UML案例解析(第三版)
- 大數(shù)據(jù)時代的企業(yè)升級之道(全3冊)
- 算法秘籍
- 3D Printing Designs:The Sun Puzzle