Changes

Apache Kafka

1,267 bytes added, 10:46, 21 April 2019
Logback.xml
<appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<!-- https://gquintana.github.io/2017/12/01/Structured-logging-with-SL-FJ-and-Logback.html -->
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"application":"log-odysseythis is the extra field"}</customFields>
</encoder>
<topic>adamtest2-topic</topic>
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.HostNameKeyingStrategy" />
A Kafka brókerek listáját a '''bootstrap.servers''' producerConfig paraméterben kell megadni. Nagyon fontos, hogy ugyan azzal a host névvel tegyük ezt ide, mint ahogy a swarm-ban létrehoztuk, és az itt megadott nevet fel kell venni a hosts-ba. A producerConfig-ok teljes listája itt olvasható: ttps://kafka.apache.org/documentation.html#producerconfigs
<source lang="xml">
<producerConfig>bootstrap.servers=kafka:9092</producerConfig></source> A '''customFields''' paraméterben tetszőleges log paramétereket adhatunk a Kafka üzenethez. <source lang="xml"><customFields>{"application":"this is the extra field"}</customFields></source> <br>===Java kód=== <source lang="java">import org.slf4j.Logger;import org.slf4j.LoggerFactory; public class LogbackExample {  private static final Logger logger = LoggerFactory.getLogger(LogbackExample.class.getSimpleName());  public static void main(String... args) throws InterruptedException { logger.info("this is the message:"); }}
</source>
 
===Futtatás===
Indítsuk el a kafka-console-consumer -t a test2-topic-ra, hogy lássuk, hogy a logback milyen üzeneteket tesz be:
<pre>
./kafka-console-consumer \
--bootstrap-server kafka:29092 \
--topic test2-topic \
--from-beginning
</pre>
 
Futtassuk le a LogbackExample java programot, ekkor a consumer ki fogja írni a logback által beküldött üzenetet:
<pre>
{"@timestamp":"2019-04-21T12:44:32.430+02:00","@version":"1","message":"this is the message:","logger_name":"LogbackExample","thread_name":"main","level":"INFO","level_value":20000,"HOSTNAME":"adamDell2","application":"this is the extra field"}
</pre>
Látható, hogy bekerült az üzenetbe a timestamp is és az extra mező is, amit a logback appender-ben adtunk hozzá.
<br>
 
===Custom log object===
 
 
<br>