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
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:Log into the ControlPanel as an administrator user and go to “Centralizad logs”:
Go to “Streams” tab:
Click “Create Stream”:
Fill the required data with the title and description that you want. By default we will leave the default index set:
Find the created Stream and click at “Manage Rules”:
Click the “add stream rule” button:
Define the rule we want to check. In our example it can be to test the field “app_name” with the value “scoringMicroservice”:
Click “I'm done” and then “start Stream”:
This wil create and activate a new Stream for our microservice.
Microservice logging configuration
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>
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:
Define default value for environment variables on dockerfile (last to variables)
ENV JAVA_OPTS="$JAVA_OPTS -Xms1G -Xmx3G" \ ... GRAYLOG_HOST=localhost \ GRAYLOG_PORT=12201
Define graylog host and port on application.yml (graylog section)
onesaitplatform: iotclient: multitenant: false ... writeTimeoutInSec: 30 readTimeoutInSec: 30 graylog: host: localhost port: 12201
Define graylog host and port on application-docker.yml (graylog section). For Docker deployments we will configure the environment variables instead of fixed values:
Add a new GELF appender to the logback configuration file. In our logback-spring.xml file we will add:
Define variables from properties: At the beginning of the file (after property tags) we will define variables for the properties values:
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.
Activate the appender:
Once the changes have been made, commit and upload to the repository
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:Build the microservice with the changes. Go to “my Microservices” and click de “build” button:
Deploy the changes. Once the new image has been built, then publish it by clicking on the “upgrade” button:
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.