How to create a microservice that writes to the centralized log tool

In this tutorial we will follow the instructions of how to configure a microservice to write to the centralized logs tool.

Instructions

  1. Centralized logs configuracion:

    We need to create a Stream in the centralized log tool. For that purpose, we will follow the next streps logged as an administrator user:


    1. Log into the ControlPanel as an administrator user and go to “Centralizad logs”:

       

    2. Go to “Streams” tab:

       

    3. Click “Create Stream”:

       

    4. Fill the required data with the title and description that you want. By default we will leave the default index set:

       

    5. Find the created Stream and click at “Manage Rules”:

       

    6. Click the “add stream rule” button:

       

    7. Define the rule we want to check. In our example it can be to test the field “app_name” with the value “scoringMicroservice”:

       

    8. Click “I'm done” and then “start Stream”:

       

      This wil create and activate a new Stream for our microservice.


  2. Microservice logging configuration

    1. Add graylog’s GELF dependency to the microservice pom.xml. In our microservice’s POM we will add the following dependency:

      <dependency> <groupId>biz.paluch.logging</groupId> <artifactId>logstash-gelf</artifactId> <version>1.14.1</version> </dependency>

       

    2. Define properties and environment variables for Graylog’s host and port. This will help us to make it easy to configure the conection to Graylog in any deployment:

      1. Define default value for environment variables on dockerfile (last to variables)

        ENV JAVA_OPTS="$JAVA_OPTS -Xms1G -Xmx3G" \ ... GRAYLOG_HOST=localhost \ GRAYLOG_PORT=12201
      2. Define graylog host and port on application.yml (graylog section)

        onesaitplatform: iotclient: multitenant: false ... writeTimeoutInSec: 30 readTimeoutInSec: 30 graylog: host: localhost port: 12201
      3. Define graylog host and port on application-docker.yml (graylog section). For Docker deployments we will configure the environment variables instead of fixed values:

         

    3. Add a new GELF appender to the logback configuration file. In our logback-spring.xml file we will add:

      1. Define variables from properties: At the beginning of the file (after property tags) we will define variables for the properties values:

         

      2. New GELF appender: Here we configure the Graylog connection. It is crucial to set the app_name to the same value as in the Stream (step ‘1.g’ on this guide). Host and port will be set by the properties or environment variables as needed.

      3. Activate the appender:


        Once the changes have been made, commit and upload to the repository

    4. Redeploy microservice and test

      Once the changes have been commited and uploaded to the repository, it is time to redeploy de micro service and test that it is sending the logs to Graylog:

      1. Build the microservice with the changes. Go to “my Microservices” and click de “build” button:

         

      2. Deploy the changes. Once the new image has been built, then publish it by clicking on the “upgrade” button:

         

      3. Here is where the environment variables come in play. In our case, the centralized log tool is running in the same CAAS, being the service “log-centralizer” on the “onesaitplatform” stack, so that is what the host will be: log-centralizer.onesaitplatform

         

 

One it has been deployed, the microservice will be sending the logs to our centralized logs tool:

 

 

available from 2.3.0-immortal version onwards

Artículos Relacionados

 

Filter by label

There are no items with the selected labels at this time.