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