Elasticsearch

How to send logs to Elasticsearch.

Key takeaways

Elasticsearch Search application. In this tutorial, it used to store log messages from different components. Pip.Services supports versions 6 and 7.
ElasticSearchLogger Component used to send log messages to Elasticsearch.
configure() Method used to set the values of configuration parameters.
open() Method used to connect to Elasticsearch.
close() Method used to close the logger and free used resources.
fatal() Method used to log a fatal level or level 1 message.
error() Method used to log an error level or level 2 message.
warn() Method used to log a warning level or level 3 message.
info() Method used to log an info level or level 4 message.
debug() Method used to log a debug level or level 5 message.
trace() Method used to log a trace level or level 6 message.

Introduction

In this tutorial you will learn how to work with the ElasticSearchLogger component, which is used to send logs to Elasticsearch. First, we will see the necessary pre-requisites. Then, we will continue by creating a custom component with an example method, running it and generating the logs. After this, we will verify that the messages reached Elasticsearch by using the Elasticvue online tool. Finally, we will improve our custom component by including several Pip.Services best practices.

ElasticSearchLogger

This component, which is a subclass of the CachedLogger component, allows us to send log messages to Elasticsearch. In order to learn how to use it, in the following sections, we will first see how to create a custom component that uses it to send log messages to Elasticsearch. Then, we will learn how to verify that the log messages have arrived. And finally, we will improve our component by adding some interfaces and methods.

Pre-requisites

In order to use this component, we must first import it. The following command shows how to do this:

import { ElasticSearchLogger } from 'pip-services4-elasticsearch-node';

import ellog "github.com/pip-services4/pip-services4-go/pip-services4-elasticsearch-go/log"

from pip_services4_elasticsearch.log import ElasticSearchLogger
Not available

Creating a component with logging

Now, we can create a custom component that sends log messages. As we want to send those messages to Elasticsearch, we define our logger of type ElasticSearchLogger. We also create a method called “mymethod” with the aim of calling it and sending log messages. All log messages are considered level 4 or Info level. The code below shows how this class looks like.

import { ElasticSearchLogger } from 'pip-services4-elasticsearch-node';
import { ILogger } from 'pip-services4-observability-node';
import { Context } from 'pip-services4-components-node';

export class MyComponentA {
    private _logger: ILogger;
    private _console_log = true;

    public constructor(ctx: Context, logger: ElasticSearchLogger) {
        this._logger = logger;

        this._logger.info(ctx, "MyComponentA has been created.");
    }

    public myMethod(ctx: Context): void {
        try {
            if (this._console_log) {
                console.log("Hola amigo");
                console.log("Bonjour mon ami");
            }
            this._logger.info(ctx, "Greetings created.")
        } finally {
            this._logger.info(ctx, "Finally reached.")
        }
    }
}

import (
	"context"
	"fmt"

	ellog "github.com/pip-services4/pip-services4-go/pip-services4-elasticsearch-go/log"
	clog "github.com/pip-services4/pip-services4-go/pip-services4-observability-go/log"
)

type MyComponentA struct {
	_logger      clog.ILogger
	_console_log bool
}

func NewMyComponentA(logger *ellog.ElasticSearchLogger) *MyComponentA {
	c := &MyComponentA{}
	c._logger = logger

	c._logger.Info(context.Background(), "MyComponentA has been created.")
	return c
}

func (c *MyComponentA) MyMethod() {
	if c._console_log {
		fmt.Println("Hola amigo")
		fmt.Println("Bonjour mon ami")
	}
	c._logger.Info(context.Background(), "Greetings created.")
}


class MyComponentA:

    _console_log = True
    
    def __init__(self, logger: ElasticSearchLogger):
        self._logger = logger

        self._logger.info(ctx, "MyComponentA has been created.")

    def mymethod(self):

        try:
            if self._console_log:
                print("Hola amigo")
                print("Bonjour mon ami")
            self._logger.info(ctx, "Greetings created.")
        finally:
                self._logger.info(ctx, "Finally reached.")
Not available

Creating a ElasticSearchLogger component

Once we have our custom component, we create an instance of the ElasticSearchLogger, and configure and connect it to Elasticsearch via the open() method. In our example, we have Elasticsearch running on our machine, and we connect to it via the port 9200. The open() method only requires a context.

let logger = new ElasticSearchLogger();

logger.configure(ConfigParams.fromTuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 9200
));

logger.open(ctx)

import cconf "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"

logger := ellog.NewElasticSearchLogger()
logger.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
	"connection.protocol", "http",
	"connection.host", "localhost",
	"connection.port", 9200,
))
err := logger.Open(context.Background())
if err != nil {
	panic(err)
}

logger = ElasticSearchLogger()
logger.configure(ConfigParams.from_tuples(
    "connection.protocol", "http",
    "connection.host", "localhost",
    "connection.port", 9200
))
logger.open(ctx)
Not available

Generating logs

Now that our connection is ready, we create an instance of our custom component and run “mymethod”. The following code shows how to do this.

let mycomponent = new MyComponentA(logger);
for (let i =0; i < 10; i++)
    mycomponent.myMethod();


mycomponent := NewMyComponentA(logger)
for i := 0; i < 10; i++ {
	mycomponent.MyMethod()
}

mycomponent = MyComponentA(logger)
for i in range(10):
    mycomponent.mymethod()
Not available

And, after running the above code, we obtain the following output.

figure 1

Our logs in Elasticsearch

We can check that Elasticsearch received our message by using a tool such as Elasticvue. This online application provides us we a UI where the received messages are displayed. The figure below shows the messages obtained after running the code in the previous section.

figure 2

Final code

Our last step is to modify our custom component. We want it to follow the Pip.Services best practices. Thus, we add the IConfigurable and IOpenable interfaces, and the configure(), open() and close() methods. Then, we use these methods to configure, open and close our logger. The code below shows how to do this.

import { ElasticSearchLogger } from 'pip-services4-elasticsearch-node';
import { ConfigParams, Context, IConfigurable, IOpenable } from 'pip-services4-components-node';

export class MyComponentA implements IConfigurable, IOpenable {
    private _logger: ElasticSearchLogger;
    private _console_log = true;

    public constructor(ctx: Context, logger: ElasticSearchLogger) {
        this._logger = logger;

        this._logger.info(ctx, "MyComponentA has been created.");
    }

    public configure(config: ConfigParams): void {
        this._logger.configure(config);
    }

    public isOpen(): boolean {
        return this._logger.isOpen();
    }

    public async open(ctx: Context): Promise<void> {
        await this._logger.open(ctx);
    }

    public async close(ctx: Context): Promise<void> {
        await this._logger.close(ctx);
    }

    public myMethod(ctx: Context): void {
        try {
            if (this._console_log) {
                console.log("Hola amigo");
                console.log("Bonjour mon ami");
            }
            this._logger.info(ctx, "Greetings created.")
        } finally {
            this._logger.info(ctx, "Finally reached.")
        }
    }
}

import (
	"context"
	"fmt"

	cconf "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
	ellog "github.com/pip-services4/pip-services4-go/pip-services4-elasticsearch-go/log"
)

func main() {
	logger := ellog.NewElasticSearchLogger()

	logger.Configure(context.Background(), cconf.NewConfigParamsFromTuples(
		"connection.protocol", "http",
		"connection.host", "localhost",
		"connection.port", 9200,
	))

	err := logger.Open(context.Background())
	if err != nil {
		panic(err)
	}

	mycomponent := NewMyComponentA(logger)
	for i := 0; i < 10; i++ {
		mycomponent.MyMethod(context.Background())
	}

}

type MyComponentA struct {
	_logger      *ellog.ElasticSearchLogger
	_console_log bool
}

func NewMyComponentA(logger *ellog.ElasticSearchLogger) *MyComponentA {
	c := &MyComponentA{}
	c._logger = logger
	c._console_log = true

	c._logger.Info(context.Background(), "MyComponentA has been created.")
	return c
}

func (c *MyComponentA) Configure(ctx context.Context, config *cconf.ConfigParams) {
	c._logger.Configure(ctx, config)
}

func (c *MyComponentA) IsOpen() bool {
	return c._logger.IsOpen()
}

func (c *MyComponentA) Open(ctx context.Context) (err error) {
	return c._logger.Open(ctx)
}

func (c *MyComponentA) Close(ctx context.Context) (err error) {
	return c._logger.Close(ctx)
}

func (c *MyComponentA) MyMethod(ctx context.Context) {
	if c._console_log {
		fmt.Println("Hola amigo")
		fmt.Println("Bonjour mon ami")
	}
	c._logger.Info(context.Background(), "Greetings created.")

	defer c._logger.Info(ctx, "Finally reached.")
}


from pip_services4_elasticsearch.log import ElasticSearchLogger
from pip_services4_components.run import IOpenable
from pip_services4_components.context import Context
from pip_services4_components.config import IConfigurable, ConfigParams
from typing import Optional
import time

class MyComponentA(IConfigurable, IOpenable):

    _console_log = True
    
    def __init__(self):
        self._logger = ElasticSearchLogger()

        self._logger.info(ctx, "MyComponentA has been created.")

    def configure(self, config: ConfigParams):
        self._logger.configure(config)
        
    def open(self, ctx: Context):
        self._logger.open(correlation_id)
       
    def close(self, ctx: Context):
        self._logger.close(correlation_id)
        
    def mymethod(self):

        try:
            if self._console_log:
                print("Hola amigo")
                print("Bonjour mon ami")
                self._logger.info(ctx, "Greetings created.")
        finally:
                self._logger.info(ctx, "Finally reached.")
Not available

Elasticsearch versions

At present, Pip.Services support versions 6 and 7 of Elaticsearch.

Wrapping up

In this tutorial we have seen how to use the ElasticSearchLogger component to send log messages to Elasticsearch. First, we created a custom component that included a method and a logger. Then, we created instances of ElasticSearchLogger and of the custom component, connected our logger to Elasticsearch and run the method in order to generate log messages. After successfully running the method, we verified that Elasticsearch had received the messages by using Elasticvue, an online application that provided us with a UI. We ended the tutorial with an improved version of the custom component.