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

com.teambytes.cloudwatch.appenders.logback.CloudwatchLogbackAppender Maven / Gradle / Ivy

The newest version!
package com.teambytes.cloudwatch.appenders.logback;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import com.amazonaws.services.logs.AWSLogsAsyncClient;
import com.amazonaws.services.logs.model.CreateLogGroupRequest;
import com.amazonaws.services.logs.model.CreateLogStreamRequest;
import com.amazonaws.services.logs.model.InputLogEvent;
import com.amazonaws.services.logs.model.InvalidSequenceTokenException;
import com.amazonaws.services.logs.model.PutLogEventsRequest;
import com.amazonaws.services.logs.model.PutLogEventsResult;
import com.amazonaws.services.logs.model.ResourceAlreadyExistsException;

public abstract class CloudwatchLogbackAppender extends AppenderBase {

  private AWSLogsAsyncClient awsClient;
  private Layout layout;
  private String logGroupName;
  private String logStreamName;
  private AtomicReference lastSequenceToken =  new AtomicReference(null);

  public CloudwatchLogbackAppender() {
    this.awsClient = new AWSLogsAsyncClient();
  }

  @Override
  public void start() {
    CreateLogGroupRequest createLogGroupRequest = new CreateLogGroupRequest(logGroupName);
    try {
      awsClient.createLogGroup(createLogGroupRequest);
    } catch(ResourceAlreadyExistsException e) {
      addInfo("Log group '" + logGroupName + "' already exists");
    }

    CreateLogStreamRequest createLogStreamRequest = new CreateLogStreamRequest(logGroupName, logStreamName);
    try {
      awsClient.createLogStream(createLogStreamRequest);
    } catch(ResourceAlreadyExistsException e) {
      addInfo("Log stream '" + logStreamName + "' in group '" + logGroupName + "' already exists");
    }

    try {
      sendEvent("Getting the next expected sequenceToken for CloudwatchAppender");
    } catch(InvalidSequenceTokenException e) {
      lastSequenceToken.set(e.getExpectedSequenceToken());
    }

    super.start();
  }

  @Override
  public void stop() {
    super.stop();
    this.awsClient.shutdown();
  }

  protected void sendEvent(String message) {
    List logEvents = new ArrayList();
    logEvents.add(new InputLogEvent().
        withTimestamp(new Date().getTime()).
        withMessage(message));
    PutLogEventsRequest putLogEventsRequest = new PutLogEventsRequest(logGroupName, logStreamName, logEvents);
    putLogEventsRequest.setSequenceToken(lastSequenceToken.get());

    PutLogEventsResult result = awsClient.putLogEvents(putLogEventsRequest);
    lastSequenceToken.set(result.getNextSequenceToken());
  }

  public void setLayout(Layout layout) {
    this.layout = layout;
  }

  public Layout getLayout() {
    return layout;
  }

  public void setLogGroupName(String logGroupName) {
    this.logGroupName = logGroupName;
  }

  public void setLogStreamName(String logStreamName) {
    this.logStreamName = logStreamName;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy