ELK Stack
Run Elasticsearch, Logstash and Kibana in Docker, create a Kibana data view and ship Java logs to Logstash with Logback.
3 min readUpdated
The ELK stack collects and searches application logs: Logstash receives log events, Elasticsearch stores and indexes them, and Kibana is the web interface for searching and charting them.
This note starts the stack with the docker-elk project, tells Kibana where the logs are, and sends logs from a Java application through Logback and the Logstash encoder.
Start the stack with docker-elk
Clone the project:
git clone https://github.com/deviantony/docker-elk.gitcd docker-elkInitialise the Elasticsearch users and their passwords (taken from the .env file), then start the stack:
docker compose up setupdocker compose upAdd -d to the second command to run in the background. Kibana is available at http://localhost:5601 after about a minute; the default user is elastic with the password changeme. Change the default passwords before you use the stack for anything but a local test; the docker-elk README describes the steps.
The stack exposes these ports:
| Port | Service |
|---|---|
| 5044 | Logstash Beats input |
| 50000 | Logstash TCP input |
| 9200 | Elasticsearch HTTP API |
| 5601 | Kibana |
Older docker-elk releases used port 5000 for the Logstash TCP input.
Create the Kibana data view
Kibana needs a data view (called an index pattern before Kibana 8) that tells it which indices to search and which field holds the event time. Where Logstash writes depends on its version:
- Logstash 8 and later, with no
indexset in the pipeline output, write to the data streamlogs-generic-default. Its backing indices matchlogs-*. - Logstash 7 and earlier write to daily indices named
logstash-YYYY.MM.dd, matched bylogstash-*.
Create the data view with the Kibana API (use logstash-* as the title on an older stack):
curl -X POST 'http://localhost:5601/api/data_views/data_view' \ -u elastic:changeme \ -H 'kbn-xsrf: true' \ -H 'Content-Type: application/json' \ -d '{"data_view":{"title":"logs-*","timeFieldName":"@timestamp"}}'The kbn-xsrf header is required for API requests that change data. On Kibana 6, the same thing was done with the saved objects API, which needed the Kibana version in a header:
curl -XPOST -D- 'http://localhost:5601/api/saved_objects/index-pattern' -H 'Content-Type: application/json' -H 'kbn-version: 6.2.3' -d '{"attributes":{"title":"logstash-*","timeFieldName":"@timestamp"}}'You can also create the data view in Kibana under Stack Management.
Send Java logs with Logback
The Logstash Logback encoder formats each log event as a JSON document and sends it to Logstash over TCP.
Add the dependencies
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version></dependency><dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.0</version></dependency>These versions date from 2018. Current releases of the encoder require a recent Logback and a direct dependency on jackson-databind; check the encoder’s README for the versions that go together.
Configure the appender
LogstashTcpSocketAppender sends every event to the Logstash TCP input. Set destination to the port your stack listens on (50000 with current docker-elk, 5000 with older releases):
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration>
<configuration debug="true"> <appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:50000</destination>
<!-- encoder is required --> <encoder class="net.logstash.logback.encoder.LogstashEncoder" /> </appender>
<root level="DEBUG"> <appender-ref ref="stash" /> </root></configuration>debug="true" makes Logback print its own configuration status at startup, which helps when events do not arrive.
The encoder writes one JSON document per line. For Logstash to parse those lines into fields, give the TCP input the json_lines codec in logstash/pipeline/logstash.conf:
input { tcp { port => 50000 codec => json_lines }}Log some events
This class writes three events at different levels ten times. Run it, then search the data view in Kibana’s Discover view:
package com.aripd.pingerbee;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
public class MainApp {
static final Logger LOG = LoggerFactory.getLogger(MainApp.class);
public static void main(String[] args) throws Exception { for (int i = 0; i < 10; i++) {
LOG.info("New customer successfully registered"); LOG.warn("User password will expire in two days"); LOG.error("Billing system is not available");
Thread.sleep(200); } }
}The appender sends events asynchronously. The Thread.sleep gives it time to flush before the program ends; in a real application, stop the Logback context cleanly on shutdown so that no events are lost.