ptgoetz/logback-kafka


Logback appenders for logging data to Apache Kafka

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.ptgoetz:logback-kafka:'
	}
	dependencies {
		implementation("com.github.ptgoetz:logback-kafka:")
	}
	<dependency>
	    <groupId>com.github.ptgoetz</groupId>
	    <artifactId>logback-kafka</artifactId>
	    <version></version>
	</dependency>

                            
    libraryDependencies += "com.github.ptgoetz" % "logback-kafka" % ""
        
        

                            
    :dependencies [[com.github.ptgoetz/logback-kafka ""]]
        
        

Readme


logback-kafka

Logback appenders for logging data to Apache Kafka

Maven Dependency

To use logback-kafka in your project add to following to your pom.xml:

<dependency>
    <groupId>com.github.ptgoetz</groupId>
    <artifactId>logback-kafka</artifactId>
    <version>0.1.0</version>
</dependency>

Configuration

To configure your application to log to kafka, add an appender entry in your logback configuration file, and specify a zookeeper host string, and kafka topic name to log to.

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="KAFKA"
        class="com.github.ptgoetz.logback.kafka.KafkaAppender">
        <topic>mytopic</topic>
        <zookeeperHost>localhost:2181</zookeeperHost>
    </appender>
    <root level="debug">
        <appender-ref ref="KAFKA" />
    </root>
</configuration>

Overriding Default Behavior

By default, the Kafka appender will simply write the received log message to the kafka queue. You can override this behavior by specifying a custom formatter class:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="KAFKA"
        class="com.github.ptgoetz.logback.kafka.KafkaAppender">
        <topic>foo</topic>
        <zookeeperHost>localhost:2181</zookeeperHost>
        <!-- specify a custom formatter -->
        <formatter class="com.github.ptgoetz.logback.kafka.formatter.JsonFormatter">
            <!-- 
            Whether we expect the log message to be JSON encoded or not.
            If set to "false", the log message will be treated as a string, 
            and wrapped in quotes. Otherwise it will be treated as a parseable
            JSON object.
            -->
            <expectJson>true</expectJson>
        </formatter>
    </appender>
    <root level="debug">
        <appender-ref ref="KAFKA" />
    </root>
</configuration>

Formatters simply need to implement the com.github.ptgoetz.logback.kafka.formatter.Formatter interface:

package com.github.ptgoetz.logback.kafka.formatter;

import ch.qos.logback.classic.spi.ILoggingEvent;

public interface Formatter {
    String format(ILoggingEvent event);
}

You can find the ch.qos.logback.classic.spi.ILoggingEvent javadoc here.