Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package au.com.itelasoft.iot;
- import com.amazonaws.auth.AWSStaticCredentialsProvider;
- import com.amazonaws.auth.BasicAWSCredentials;
- import com.amazonaws.services.logs.AWSLogsClient;
- import com.amazonaws.services.logs.AWSLogsClientBuilder;
- import com.amazonaws.services.logs.model.DescribeLogStreamsRequest;
- import com.amazonaws.services.logs.model.DescribeLogStreamsResult;
- import com.amazonaws.services.logs.model.InputLogEvent;
- import com.amazonaws.services.logs.model.PutLogEventsRequest;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class AwsCloudWatch implements CloudLogger {
- private static volatile List<InputLogEvent> logEvents = new ArrayList();
- private static AwsCloudWatch instance;
- private static volatile Boolean inflight = false;
- private Thread awsWriteThread;
- private final BasicAWSCredentials awsCreds;
- private final AWSLogsClient cwClient;
- private final String logStreamName = "access-log";
- private final String logStreamGroup = "/test";
- public static AwsCloudWatch getInstance() {
- synchronized (AwsCloudWatch.class) {
- if (!(AwsCloudWatch.instance instanceof AwsCloudWatch)) {
- AwsCloudWatch.instance = new AwsCloudWatch();
- }
- }
- return AwsCloudWatch.instance;
- }
- private AwsCloudWatch() {
- this.awsCreds = new BasicAWSCredentials("xxxxxx", "xxxxxxxxxxxxxxxxxxxxxx");
- this.cwClient = (AWSLogsClient) AWSLogsClientBuilder.standard()
- .withCredentials(new AWSStaticCredentialsProvider(this.awsCreds))
- .withRegion("ap-southeast-1").build();
- this.initThread();
- }
- private void initThread(){
- this.awsWriteThread = new Thread(new Runnable() {
- @Override
- public synchronized void run() {
- try {
- while(true){
- writeToCloud();
- }
- } catch (Exception e) {
- System.err.println("Error logging "+e.toString());
- }
- }
- });
- this.awsWriteThread.setName("AWS Log writer thread");
- this.awsWriteThread.start();
- }
- @Override
- public void write(String message) {
- InputLogEvent logEvent = new InputLogEvent().withMessage(message).withTimestamp(System.currentTimeMillis());
- AwsCloudWatch.logEvents.add(logEvent);
- if(this.awsWriteThread.isAlive()){
- synchronized(this.awsWriteThread){
- this.awsWriteThread.notify();
- }
- }else{
- this.awsWriteThread.start();
- }
- }
- private synchronized void writeToCloud(){
- if(!AwsCloudWatch.logEvents.isEmpty() && AwsCloudWatch.inflight == false){
- AwsCloudWatch.inflight = true;
- PutLogEventsRequest putReq = new PutLogEventsRequest(this.logStreamGroup, this.logStreamName, logEvents)
- .withSequenceToken(this.getUploadSequenceToken());
- AwsCloudWatch.logEvents.removeAll(putReq.getLogEvents());
- this.cwClient.putLogEvents(putReq);
- AwsCloudWatch.inflight = false;
- }else{
- if(AwsCloudWatch.inflight == false){
- try {
- synchronized(this.awsWriteThread){
- this.awsWriteThread.wait();
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(AwsCloudWatch.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- private String getUploadSequenceToken() {
- DescribeLogStreamsRequest req = new DescribeLogStreamsRequest().withLogGroupName(logStreamGroup).withLogStreamNamePrefix(logStreamName);
- DescribeLogStreamsResult res = this.cwClient.describeLogStreams(req);
- return res.getLogStreams().get(0).getUploadSequenceToken();
- }
- }
Add Comment
Please, Sign In to add comment