官术网_书友最值得收藏!

  • 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:

  1. 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 ...]
    
  2. 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:

  1. 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 word error:
    $ heroku logs | grep error
    
  2. 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"
    
  3. 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
    
  4. 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

主站蜘蛛池模板: 永和县| 永善县| 长子县| 湖南省| 石门县| 平凉市| 德阳市| 无为县| 石阡县| 克山县| 册亨县| 西和县| 伊春市| 广丰县| 尼勒克县| 锡林郭勒盟| 临泽县| 任丘市| 黄大仙区| 布尔津县| 德令哈市| 拉萨市| 鄂托克前旗| 建阳市| 南昌县| 德安县| 桦川县| 喀喇| 梁河县| 工布江达县| 渭源县| 凤山县| 海门市| 张家口市| 阿拉善右旗| 娱乐| 邵武市| 阿勒泰市| 花莲县| 岳阳市| 建湖县|