Skip to main content
In this guide, we will demonstrate how to collect logs generated by a Node.js application running on two nodes. We will configure Promtail to gather these logs and forward them to a Loki server for visualization in Grafana.

Connecting to the Nodes

Start by opening separate terminal sessions for node-1 and node-2. Connect to the nodes using your preferred method. For example, you might use the following command:
After connecting, rename your terminal tabs to easily distinguish between node-1 and node-2. When you connect to node-1, you should see output similar to this:

Examining the Application and Log File

On node-1, navigate to the application directory where your Node.js application is running. The application writes its logs to a file called app.log, located in the same directory as index.js.
To inspect the log file, you can use the cat or tail command:
The same file structure exists on node-2, and its logs can be inspected similarly. New log entries should be created every few seconds.

Verifying Log Output

Below is an example of log entries generated on node-2:
This output confirms that the application is emitting logs. Promtail will collect these logs and forward them to the Loki server for further analysis in Grafana.

Updating the Promtail Configuration

Before updating Promtail’s configuration, stop the running Promtail instance on node-1 by pressing Ctrl-C. Then, review the current Promtail configuration file:
The default configuration might look like this:
Since your Node.js application writes logs in its directory (for example, /home/vagrant/app/app.log), add a new scrape job for these logs. Update the configuration as shown below:
You can add additional labels under the labels section (for example, env: production) to help refine log queries in Grafana.
Save the updated configuration file and exit your text editor.

Applying the Configuration on Node-1 and Node-2

On node-1, ensure you are in the correct directory before editing the configuration:
Then, open the configuration file (using an editor like nano):
Paste the updated configuration and save the file. Repeat these steps on node-2:
Make sure node-2’s configuration matches the updated settings.

Restarting Promtail

After updating the configuration on both nodes, restart Promtail. On node-2, execute:
Then, on node-1, start Promtail similarly:
Ensure the startup logs display no errors. Look for messages indicating that Promtail is reloading the configuration and listening on the designated HTTP and gRPC ports.
Example startup output:

Viewing Logs in Grafana

After Promtail is running on both nodes, access your Grafana instance and open the Explore interface. To filter the API logs, run the following query:
This query displays logs from your API application along with metadata such as hostname, HTTP method, request route, and status code. For a more specific search, you can use:
Sample log entries might appear as follows:
Grafana’s Explore interface provides filtering options and dropdowns to refine your queries based on specific log fields. The detailed log entries and visual summaries make it easier to identify issues and monitor your application logs effectively.
The image shows a Grafana Loki interface displaying log data with a graph of log volume over time and detailed log entries below.

By following these steps, you have successfully configured Promtail on multiple nodes to collect log files from both system directories and your application directory. This setup makes monitoring and troubleshooting your API logs through Grafana an efficient process.

Watch Video