- Heroku Cookbook
- Mike Coutermarsh
- 717字
- 2021-08-05 17:14:28
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:
- 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
orSTDERR
. If we're not seeing logs, it's very likely our application is not configured correctly. - 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.
- 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.
- 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
- 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
- We can use this for any process type; we can try it for our workers if we'd like:
$ heroku logs -p worker
- 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
- To view logs for only our application, we can set the source to
app
:$ heroku logs --source app
- 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
- We can even combine the different flags. Let's try tailing the logs for only our web dynos:
$ heroku logs -p web --tail
- 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
- Oracle Exadata性能優(yōu)化
- 新手學Visual C# 2008程序設計
- 精通軟件性能測試與LoadRunner實戰(zhàn)(第2版)
- Oracle從入門到精通(第5版)
- HTML5從入門到精通 (第2版)
- Flutter跨平臺開發(fā)入門與實戰(zhàn)
- Unity 2018 Shaders and Effects Cookbook
- 鴻蒙OS應用編程實戰(zhàn)
- ExtJS Web應用程序開發(fā)指南第2版
- PhoneGap 4 Mobile Application Development Cookbook
- Android Studio開發(fā)實戰(zhàn):從零基礎到App上線 (移動開發(fā)叢書)
- 軟件工程與UML案例解析(第三版)
- Kotlin進階實戰(zhàn)
- H5頁面設計與制作(全彩慕課版·第2版)
- Java Web入門很輕松(微課超值版)