Easy Method to Send PM2 Logs to Elasticsearch with pm2-elasticsearch-logger

htooaung shinehtooaung shine
3 min read

Introduction

The world of software development and deployment is becoming more complex, and effective logs management systems are essential. One popular option for managing production processes for Node.js projects is PM2. This blog post will demonstrate how to use pm2-elasticsearch-logger, an efficient tool for indexing and searching log data, to send PM2 logs to elasticsearch.

Before we dive into the specifics of sending PM2 logs to Elastic Search, it's important to have the necessary tools installed on your system. PM2 can be installed using npm (Node Package Manager), a package manager for the JavaScript runtime environment Node.js. Thanks to PM2 , it can be installed on Linux , Windows and MacOS.

PM2 or Process Manager 2, is an Open Source, production ready Node.js process manager. Some key features of PM2 are automatic application load balancing, declarative application configuration, deployment system and monitoring. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Pre-requisites

Before we go to the installation steps , it would be good if you have some knowledge about JavaScript, Node.js, and logging system. But not limited to, in order to understand the practical example I'll be giving shortly.

  • Nodejs

  • Elasticsearch (logging system)

  • Linux

Abstract

In this section, I will share the workflow that I am trying to implement. Firstly , I will try to deploy a simple Node.js app with PM2. And then , try to send these application logs to elasticsearch via the pm2 elasticsearch-logger module.

Installing PM2 and NPM Tools

First, update the package list on our system. Now I will install it on Ubuntu 22.0.4. Then, I will provide step-by-step instructions on how to install PM2 and npm.

  1. update Package List

     sudo apt update
    
  2. Install nodejs

     sudo apt install nodejs
    
  3. Now install npm. NPM is a version control tools for nodejs. we will need npm to install the require package to sends logs to elasticsearch from our nodejs application.

     sudo apt install npm
    
  4. Then , let's install pm2 with npm

     sudo npm install pm2
    
  5. Deploy nodejs application with pm2. In this step, just used simple nodejs application. You can replace your nodejs application. Let's install the required node packages with npm. There will be some difference in application js file. In this case, my application js file name is index.js.

     sudo npm install
     pm2 start index.js
    

  6. Install elasticsearch logger package with npm

     sudo npm install pm2-elasticsearch-logger
    
  7. Let's create pm2-elasticsearch-logger module with pm2 which is the package installed with npm.

     sudo pm2 install pm2-elasticsearch-logger
    

    Now we can see like that. This module will help us send pm2 logs to elastic search.

  8. Now let's configure pm2-elasticsearch-logger.

    This command helps us to send logs to our local elasticsearch server index name with nodejs*. So now we can check logs in elasticsearch index name with nodejs*. There are many options we can configure for elastic-search-logger.

     pm2 set pm2-elasticsearch-logger:elasticUrl https://localhost:9200 index nodejs*
    

There are some other options to set values for this logger module.

Summary

Effective logs management is crucial in today's complex software development and deployment landscapes. Tools like PM2, Elastic Search, and pm2-elasticsearch-logger make it easier to manage, index, and search through log data. By sending PM2 logs to Elastic Search, developers can gain valuable insights into their applications, helping them identify and resolve issues more quickly.

Reference

If you want to explore more with PM2, you can read here.

1
Subscribe to my newsletter

Read articles from htooaung shine directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

htooaung shine
htooaung shine