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

Viewing application logs

Logging gets a little more complex for any application that is running multiple servers and several different types of processes. Having visibility into everything that is happening within our application is critical to maintaining it. Heroku handles this by combining and sending all of our logs to one place, the Logplex.

The Logplex provides us with a single location to view a stream of our logs across our entire application. In this recipe, we'll learn how to view logs via the CLI. We'll learn how to quickly get visibility into what's happening within our application.

Note

We'll learn more about the Logplex and how to set up log storage in Chapter 5, Error Monitoring and Logging Tools.

How to do it…

To start, let's open up a terminal, navigate to an existing Heroku application, and perform the following steps:

  1. First, to view our applications logs, we can use the logs command:
    $ heroku logs
    2014-03-31T23:35:51.195150+00:00 app[web.1]: Rendered pages/about.html.slim within layouts/application (25.0ms)
    2014-03-31T23:35:51.215591+00:00 app[web.1]: Rendered layouts/_navigation_links.html.erb (2.6ms)
    2014-03-31T23:35:51.230010+00:00 app[web.1]: Rendered layouts/_messages.html.slim (13.0ms)
    2014-03-31T23:35:51.215967+00:00 app[web.1]: Rendered layouts/_navigation.html.slim (10.3ms)
    2014-03-31T23:35:51.231104+00:00 app[web.1]: Completed 200 OK in 109ms (Views: 65.4ms | ActiveRecord: 0.0ms)
    2014-03-31T23:35:51.242960+00:00 heroku[router]: at=info method=GET path=
    

    Note

    Heroku logs anything that our application sends to STDOUT or STDERR. If we're not seeing logs, it's very likely our application is not configured correctly.

  2. We can also watch our logs in real time. This is known as tailing:
    $ heroku logs --tail
    

    Note

    Instead of --tail, we can also use -t.

    We'll need to press Ctrl + C to end the command and stop tailing the logs.

  3. If we want to see the 100 most recent lines, we can use -n:
    $ heroku logs -n 100
    

    Note

    The Logplex stores a maximum of 1500 lines. To view more lines, we'll have to set up a log storage. We'll learn how to do this in Chapter 5, Error Monitoring and Logging Tools.

  4. We can filter the logs to only show a specific process type. Here, we will only see logs from our web dynos:
    $ heroku logs -p web
    
  5. If we want, we can be as granular as showing the logs from an individual dyno. This will show only the logs from the second web dyno:
    $ heroku logs -p web.2
    
  6. We can use this for any process type; we can try it for our workers if we'd like:
    $ heroku logs -p worker
    
  7. The Logplex contains more than just logs from our application. We can also view logs generated by Heroku or the API. Let's try changing the source to Heroku to only see the logs generated by Heroku. This will only show us logs related to the router and resource usage:
    $ heroku logs --source heroku
    
  8. To view logs for only our application, we can set the source to app:
    $ heroku logs --source app
    
  9. We can also view logs from the API. These logs will show any administrative actions we've taken, such as scaling dynos or changing configuration variables. This can be useful when multiple developers are working on an application:
    $ heroku logs --source api
    
  10. We can even combine the different flags. Let's try tailing the logs for only our web dynos:
    $ heroku logs -p web --tail
    
  11. That's it! Remember that if we ever need more information on how to view logs via the CLI, we can always use the help command:
    $ heroku help logs
    

How it works

Under the covers, the Heroku CLI simply passes our request to Heroku's API and then uses Ruby to parse and display our logs. If you're interested in exactly how it works, the code is open source on GitHub at https://github.com/heroku/heroku/blob/master/lib/heroku/command/logs.rb.

Viewing logs via the CLI is most useful in situations where we need to see exactly what our application is doing right now. We'll find that we use it a lot around deploys and when debugging issues. Since the Logplex has a limit of 1500 lines, it's not meant to view any historical data. For this, we'll need to set up log drains and enable a logging add-on. We'll be learning how to do this in Chapter 5, Error Monitoring and Logging Tools.

See also

  • To learn how to keep and search historical logs, take a look at Chapter 5, Error Monitoring and Logging Tools
主站蜘蛛池模板: 关岭| 喜德县| 苗栗市| 八宿县| 漳浦县| 宝鸡市| 威宁| 文安县| 公主岭市| 恩施市| 中江县| 子长县| 麟游县| 九寨沟县| 阳东县| 巴中市| 方城县| 舞阳县| 休宁县| 修水县| 东乡县| 青岛市| 沧源| 吉安县| 乌拉特中旗| 平果县| 莆田市| 高邑县| 新沂市| 临安市| 银川市| 惠来县| 康马县| 永嘉县| 万盛区| 定兴县| 永兴县| 拉萨市| 宿州市| 仁布县| 兴和县|