Code Monkey home page Code Monkey logo

spring-boot-log-to-kafka-example's Introduction

Spring Boot: how to log to Kafka

Kafka is ideal for log aggregation, particularly for applications that use microservices and are distributed across multiple hosts.

This is an example Spring Boot application that uses Log4j2's Kafka appender to send JSON formatted log messages to a Kafka topic.

There is now a Logging path and an Audit path. The path is determined by the presense of an SLF4J "Audit" Marker.

logging and audig paths

This application runs for 10 seconds.

  • LogMessageGenerator sends some log messages to a log Kafka topic that's defined in src/main/resources/log4j2.xml. Here's an example log message:
    {
      "timeMillis" : 1485736021946,
      "thread" : "pool-2-thread-1",
      "level" : "INFO",
      "loggerName" : "io.woolford.LogMessageGenerator",
      "message" : "Log message generated %n",
      "endOfBatch" : false,
      "loggerFqcn" : "org.apache.logging.slf4j.Log4jLogger",
      "threadId" : 17,
      "threadPriority" : 5
    }
  • AuditMessageGenerator sends audit messages to an audit Kafka topic that is defined in src/main/resources/log4j2.xml. Here's an example log messages
    {
      "message" : "Audit Message Generated",
      "eventIndex": 1
      "eventTimestamp": 1485736021946,
    }

Here's a two-minute video that walks through the code:

logging to Kafka from Spring video walk-through

And a couple 10 minute videos:

Java Logs and audit to Kafka via Logging - Part 1 Viewing Logs and audit in Kafka with KSQL - Part 2

Docker

You can bring up a local Kafka cluster in Docker using the docker-compose.yml file in local

  docker-compose up

and

  docker-compose down --remove-orphans

To do

  1. Inject spring properties for the kafka host names and kafka topic names
  2. include the host name in the log message. Cannot be done in the pattern
  3. Understand what behavior should be if Appender cannot reach the broker

Audit code

Audit code is isolated in the audit package because it was elaborated on more than was probably needed

Questions

Is the Kafka Appender production hardened for the various broker-down scenarios?

spring-boot-log-to-kafka-example's People

Contributors

alimjz avatar

Stargazers

sahar avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.