private String createLogGroupAndLogStreamIfNeeded() { LOGGER.debug("Attempting to locate the log group [{}]", logGroupName); final DescribeLogGroupsResult describeLogGroupsResult = awsLogsClient.describeLogGroups(new DescribeLogGroupsRequest().withLogGroupNamePrefix(logGroupName)); boolean createLogGroup = true; if (describeLogGroupsResult != null && describeLogGroupsResult.getLogGroups() != null && !describeLogGroupsResult.getLogGroups().isEmpty()) { createLogGroup = !describeLogGroupsResult.getLogGroups().stream().anyMatch(g -> g.getLogGroupName().equals(logGroupName)); } if (createLogGroup) { LOGGER.debug("Creating log group [{}]", logGroupName); final CreateLogGroupRequest createLogGroupRequest = new CreateLogGroupRequest(logGroupName); awsLogsClient.createLogGroup(createLogGroupRequest); } String logSequenceToken = null; boolean createLogStream = true; LOGGER.debug("Attempting to locate the log stream [{}] for group [{}]", logStreamName, logGroupName); final DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest(logGroupName).withLogStreamNamePrefix(logStreamName); final DescribeLogStreamsResult describeLogStreamsResult = awsLogsClient.describeLogStreams(describeLogStreamsRequest); if (describeLogStreamsResult != null && describeLogStreamsResult.getLogStreams() != null && !describeLogStreamsResult.getLogStreams().isEmpty()) { for (final LogStream ls : describeLogStreamsResult.getLogStreams()) { if (logStreamName.equals(ls.getLogStreamName())) { createLogStream = false; logSequenceToken = ls.getUploadSequenceToken(); LOGGER.debug("Found log stream [{}] with sequence token [{}]", logStreamName, logSequenceToken); break; } } } if (createLogStream) { LOGGER.debug("Creating log stream [{}] for group [{}]", logStreamName, logGroupName); final CreateLogStreamRequest createLogStreamRequest = new CreateLogStreamRequest(logGroupName, logStreamName); awsLogsClient.createLogStream(createLogStreamRequest); } return logSequenceToken; }
private void verifyLogGroupExists() { DescribeLogGroupsRequest request = new DescribeLogGroupsRequest().withLogGroupNamePrefix(logGroup); DescribeLogGroupsResult result = awsLogsClient.describeLogGroups(request); for (LogGroup group : result.getLogGroups()) { if (logGroup.equals(group.getLogGroupName())) { return; } } if (createLogDests) { callLogClientMethod("createLogGroup", new CreateLogGroupRequest(logGroup)); } else { logWarn("Log-group '" + logGroup + "' doesn't exist and not created", null); } }
public static void main(String[] args) { AWSLogs client = AWSLogsClientBuilder.standard().withRegion("eu-west-1").build(); // Harvest all the log groups in this region DescribeLogGroupsRequest describeLogGroupsRequest = new DescribeLogGroupsRequest(); DescribeLogGroupsResult describeLogGroupsResult = client .describeLogGroups(describeLogGroupsRequest); List<LogGroup> logGroups = describeLogGroupsResult.getLogGroups(); String token = describeLogGroupsResult.getNextToken(); int index = 0; while (token != null) { index++; describeLogGroupsRequest.setNextToken(token); describeLogGroupsResult = client.describeLogGroups(describeLogGroupsRequest); logGroups.addAll(describeLogGroupsResult.getLogGroups()); token = describeLogGroupsResult.getNextToken(); System.out.println(index); System.out.println(token); } // Delete each log group logGroups.stream().forEach( (logGroup) -> { DeleteLogGroupRequest deleteLogGroupRequest = new DeleteLogGroupRequest(logGroup .getLogGroupName()); try { RetryHelper.DoWithRetries(() -> client.deleteLogGroup(deleteLogGroupRequest), AmazonServiceException.class, Optional.of("429"), (logString) -> { }); } catch (Exception e) { e.printStackTrace(); } }); }
@Test public void testInitializationErrorHandling() throws Exception { initialize("TestCloudWatchAppender/testInitializationErrorHandling.properties"); MockCloudWatchClient mockClient = new MockCloudWatchClient() { @Override protected DescribeLogGroupsResult describeLogGroups( DescribeLogGroupsRequest request) { throw new TestingException("not now, not ever"); } }; // note that we will be running the writer on a separate thread appender.setThreadFactory(new DefaultThreadFactory()); appender.setWriterFactory(mockClient.newWriterFactory()); // first message triggers writer creation logger.debug("message one"); waitForInitialization(); AbstractLogWriter writer = (AbstractLogWriter)appender.getWriter(); MessageQueue messageQueue = appender.getMessageQueue(); assertEquals("describeLogGroups: invocation count", 1, mockClient.describeLogGroupsInvocationCount); assertEquals("describeLogStreams: invocation count", 0, mockClient.describeLogStreamsInvocationCount); assertTrue("initialization message was non-blank", ! writer.getInitializationMessage().equals("")); assertEquals("initialization exception retained", TestingException.class, writer.getInitializationException().getClass()); assertEquals("initialization error message", "not now, not ever", writer.getInitializationException().getMessage()); assertEquals("message queue set to discard all", 0, messageQueue.getDiscardThreshold()); assertEquals("message queue set to discard all", DiscardAction.oldest, messageQueue.getDiscardAction()); assertEquals("messages in queue (initial)", 1, messageQueue.toList().size()); // trying to log another message should clear the queue logger.info("message two"); assertEquals("messages in queue (second try)", 0, messageQueue.toList().size()); }