All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.byu.hbll.logback.kafka.KafkaAppender Maven / Gradle / Ivy

package edu.byu.hbll.logback.kafka;

import java.io.StringReader;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

/**
 * Class which extends appender for Kafka logging.
 * 
 * @author rodzi
 *
 */
public class KafkaAppender extends AppenderBase {

  private final int DEFAULT_LINGER_MS = 100;

  @SuppressWarnings("rawtypes")
  private KafkaProducer producer;
  private String kafkaProducerProperties;
  private String topic;
  private String logName;

  private PatternLayout layout;

  /**
   * Default constructor.
   */
  public KafkaAppender() {

  }

  @Override
  public void start() {
    super.start();
    final Properties properties = new Properties();
    try {
      properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
          "org.apache.kafka.common.serialization.StringSerializer");
      properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
          "org.apache.kafka.common.serialization.StringSerializer");
      properties.put(ProducerConfig.LINGER_MS_CONFIG, DEFAULT_LINGER_MS);
      properties.load(new StringReader(kafkaProducerProperties));
      producer = new KafkaProducer<>(properties);
    } catch (Exception exception) {
      addError("KafkaAppender: Exception initializing Producer. " + exception + " : " + exception.getMessage());
      throw new IllegalStateException("KafkaAppender: Exception initializing Producer.", exception);
    }
    addInfo("KafkaAppender: Producer initialized: " + producer);
    if (topic == null) {
      addWarn("KafkaAppender requires a topic. Add this to the appender configuration.");
    } else {
      addInfo("KafkaAppender will publish messages to the '" + topic + "' topic.");
    }
    addInfo("Kafka appender has finished configuring.");
  }

  @Override
  public void stop() {
    super.stop();
    producer.close();
    layout.stop();
  }

  @SuppressWarnings("unchecked")
  @Override
  protected void append(ILoggingEvent event) {
    try {
      ProducerRecord producerRecord = new ProducerRecord<>(topic, logName, layout.doLayout(event));
      producer.send(producerRecord);
    } catch (Exception e) {
      System.out.println("KafkaAppender: Exception sending message: '" + e + " : " + e.getMessage() + "'.");
      e.printStackTrace();
    }
  }

  /**
   * Getter.
   * 
   * @return topic.
   */
  public String getTopic() {
    return topic;
  }

  /**
   * Setter.
   * 
   * @param topic
   *          the kafka topic.
   */
  public void setTopic(String topic) {
    this.topic = topic;
  }

  /**
   * Getter.
   * 
   * @return kafkaProducerProperties.
   */
  public String getKafkaProducerProperties() {
    return kafkaProducerProperties;
  }

  /**
   * Setter.
   * 
   * @param kafkaProducerProperties
   *          the producer properties.
   */
  public void setKafkaProducerProperties(String kafkaProducerProperties) {
    this.kafkaProducerProperties = kafkaProducerProperties;
  }

  /**
   * Getter for logName.
   * 
   * @return logName.
   */
  public String getLogName() {
    return logName;
  }

  /**
   * Setter for logName.
   * 
   * @param logName
   *          the log name.
   */
  public void setLogName(String logName) {
    this.logName = logName;
  }

  /**
   * Getter for pattern layout.
   * 
   * @return pattern layout.
   */
  public PatternLayout getLayout() {
    return layout;
  }

  /**
   * Setter for the pattern layout.
   * 
   * @param layout
   *          the pattern layout.
   */
  public void setLayout(PatternLayout layout) {
    this.layout = layout;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy