- 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
- Python從菜鳥到高手(第2版)
- ASP.NET動態網頁設計教程(第三版)
- TypeScript實戰指南
- Advanced Oracle PL/SQL Developer's Guide(Second Edition)
- SQL Server 2016數據庫應用與開發
- Learning OpenStack Networking(Neutron)
- Multithreading in C# 5.0 Cookbook
- 常用工具軟件立體化教程(微課版)
- Node.js 12實戰
- WordPress Search Engine Optimization(Second Edition)
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- 3D Printing Designs:The Sun Puzzle
- 虛擬現實:引領未來的人機交互革命
- Python編程:從入門到實踐(第2版)
- Python數據分析與挖掘實戰(第2版)