private void fetchCloudWatchLogs(String logStreamName, PrintStream logger) { if(logStreamName == null || "".equals(logStreamName)) return; logger.println("Fetching logs from cloudwatch logs for final attempt..."); logger.println("-------------------------------------------------------"); AWSLogs awslogs = AWSLogsClientBuilder.defaultClient(); GetLogEventsResult logEventsResult = awslogs.getLogEvents( new GetLogEventsRequest() .withLogGroupName("/aws/batch/job") .withLogStreamName(logStreamName) ); for(OutputLogEvent ole : logEventsResult.getEvents()) { logger.printf("[%s] %s%n", df.format(new Date(ole.getTimestamp())), ole.getMessage()); } }
@Override public void activateOptions() { super.activateOptions(); if (isBlank(logGroupName) || isBlank(logStreamName)) { Logger.getRootLogger().error("Could not initialise CloudwatchAppender because either or both LogGroupName(" + logGroupName + ") and LogStreamName(" + logStreamName + ") are null or empty"); this.close(); } else { this.awsLogsClient = AWSLogsClientBuilder .standard() .withRegion(this.getAwsRegion()) .build(); loggingEventsQueue = new LinkedBlockingQueue<>(queueLength); try { initializeCloudwatchResources(); keepDaemonActive.set(true); initCloudwatchDaemon(); cloudwatchAppenderInitialised.set(true); } catch (Exception e) { Logger.getRootLogger().error("Could not initialise Cloudwatch Logs for LogGroupName: " + logGroupName + " and LogStreamName: " + logStreamName, e); if (DEBUG_MODE) { System.err.println("Could not initialise Cloudwatch Logs for LogGroupName: " + logGroupName + " and LogStreamName: " + logStreamName); e.printStackTrace(); } } } }
/** * Loads the test-specific Log4J configuration and resets the environment. */ public void setUp(String propertiesName, String logGroupName) throws Exception { URL config = ClassLoader.getSystemResource(propertiesName); assertNotNull("missing configuration: " + propertiesName, config); LogManager.resetConfiguration(); PropertyConfigurator.configure(config); localLogger = Logger.getLogger(getClass()); localClient = AWSLogsClientBuilder.defaultClient(); deleteLogGroupIfExists(logGroupName); }
@Override public synchronized void start() { if (isStarted()) { return; } if (layout == null) { layout = new EchoLayout<>(); addStatus(new WarnStatus("No layout, default to " + layout, this)); } if (logGroupName == null) { logGroupName = getClass().getSimpleName(); addStatus(new WarnStatus("No logGroupName, default to " + logGroupName, this)); } if (logStreamName == null) { logStreamName = new SimpleDateFormat("yyyyMMdd'T'HHmmss").format(new Date()); addStatus(new WarnStatus("No logGroupName, default to " + logStreamName, this)); } try { if (awsLogs == null) { awsLogs = AWSLogsClientBuilder.defaultClient(); } createLogGroup(); createLogStream(); } catch (final AmazonClientException e) { awsLogs = null; addStatus(new ErrorStatus(e.getMessage(), this, e)); } // Start a new daemon time thread to periodically upload events // Because this is a deamon thread, it will not block shutdown new DaemonTimerThread().start(); // Add a shutdown hook to catch any final events at shutdown Runtime.getRuntime().addShutdownHook(new ShutdownHook()); layout.start(); super.start(); }
static AWSLogs createLogsClient(CloudwatchLogsConfig config) { AWSLogsClientBuilder builder = AWSLogsClientBuilder.standard(); if (config.getEndpoint() != null) { // Non-AWS mock endpoint builder.setCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials())); builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(config.getEndpoint(), config.getRegion())); } else { builder.setRegion(config.getRegion()); } return builder.build(); }
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(); } }); }
public static void main(String[] args) { final String USAGE = "To run this example, supply:\n" + "* a filter name\n" + "* filter pattern\n" + "* log group name\n" + "* lambda function arn\n\n" + "Ex: PutSubscriptionFilter <filter-name> \\\n" + " <filter pattern> \\\n" + " <log-group-name> \\\n" + " <lambda-function-arn>\n"; if (args.length != 4) { System.out.println(USAGE); System.exit(1); } String filter = args[0]; String pattern = args[1]; String log_group = args[2]; String function_arn = args[3]; final AWSLogs cwl = AWSLogsClientBuilder.defaultClient(); PutSubscriptionFilterRequest request = new PutSubscriptionFilterRequest() .withFilterName(filter) .withFilterPattern(pattern) .withLogGroupName(log_group) .withDestinationArn(function_arn); PutSubscriptionFilterResult response = cwl.putSubscriptionFilter(request); System.out.printf( "Successfully created CloudWatch logs subscription filter %s", filter); }
public static void main(String[] args) { final String USAGE = "To run this example, supply a filter name and log group name\n" + "Ex: DeleteSubscriptionFilter <filter-name> <log-group-name>\n"; if (args.length != 2) { System.out.println(USAGE); System.exit(1); } String filter = args[0]; String log_group = args[1]; final AWSLogs logs = AWSLogsClientBuilder.defaultClient(); DeleteSubscriptionFilterRequest request = new DeleteSubscriptionFilterRequest() .withFilterName(filter) .withLogGroupName(log_group); DeleteSubscriptionFilterResult response = logs.deleteSubscriptionFilter(request); System.out.printf( "Successfully deleted CloudWatch logs subscription filter %s", filter); }
/** * This function is used as a client factory by the smoketest. */ public static AWSLogs createClient() { return AWSLogsClientBuilder.defaultClient(); }
public CloudwatchMetricFilterPublisher(Log log) { // http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default this.client = AWSLogsClientBuilder.defaultClient(); this.log = log; }
/** * Returns a CloudwatchLogs service client. * * <p>This method is provided so unit tests can mock out CloudwatchLogs. */ public AWSLogs getAWSLogsClient(String region) { // Use a getter here so unit tests can substitute a mock client AWSLogs client = AWSLogsClientBuilder.standard().withRegion(region).build(); return client; }
public static void main(String[] args) { final String USAGE = "To run this example, supply a log group name\n" + "Ex: DescribeSubscriptionFilters <log-group-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String log_group = args[0]; final AWSLogs logs = AWSLogsClientBuilder.defaultClient(); boolean done = false; DescribeSubscriptionFiltersRequest request = new DescribeSubscriptionFiltersRequest() .withLogGroupName(log_group) .withLimit(1); while(!done) { DescribeSubscriptionFiltersResult response = logs.describeSubscriptionFilters(request); for(SubscriptionFilter filter : response.getSubscriptionFilters()) { System.out.printf( "Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", filter.getFilterName(), filter.getFilterPattern(), filter.getDestinationArn()); } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } } }