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

com.teambytes.cloudwatch.appenders.log4j.CloudwatchLog4jAppender Maven / Gradle / Ivy

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

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

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;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public abstract class CloudwatchLog4jAppender  extends AppenderSkeleton {

  private AWSLogsAsyncClient awsClient;
  private String logGroupName;
  private String logStreamName;
  private AtomicBoolean activated =  new AtomicBoolean(false);
  private AtomicReference lastSequenceToken =  new AtomicReference(null);

  protected CloudwatchLog4jAppender() {
    this.awsClient = new AWSLogsAsyncClient();
  }

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

    CreateLogStreamRequest createLogStreamRequest = new CreateLogStreamRequest(logGroupName, logStreamName);
    try {
      awsClient.createLogStream(createLogStreamRequest);
    } catch(ResourceAlreadyExistsException e) {
      System.out.println("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.activateOptions();
    activated.set(true);
  }

  @Override
  protected final void append(LoggingEvent loggingEvent) {
    if(!activated.get()) {
      activateOptions();
    }

    sendEvent(doAppendSendEvent(loggingEvent));
  }

  protected abstract String doAppendSendEvent(LoggingEvent loggingEvent);

  protected final 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());
  }

  @Override
  public final void close() {
    awsClient.shutdown();
  }

  @Override
  public final boolean requiresLayout() {
    return true;
  }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy