Search in sources :

Example 1 with AWSLogs

use of com.amazonaws.services.logs.AWSLogs in project aws-doc-sdk-examples by awsdocs.

the class PutSubscriptionFilter method main.

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);
}
Also used : PutSubscriptionFilterResult(com.amazonaws.services.logs.model.PutSubscriptionFilterResult) PutSubscriptionFilterRequest(com.amazonaws.services.logs.model.PutSubscriptionFilterRequest) AWSLogs(com.amazonaws.services.logs.AWSLogs)

Example 2 with AWSLogs

use of com.amazonaws.services.logs.AWSLogs in project ranger by apache.

the class AmazonCloudWatchAuditDestination method log.

@Override
public synchronized boolean log(Collection<AuditEventBase> collection) {
    boolean ret = false;
    AWSLogs client = getClient();
    PutLogEventsRequest req = new PutLogEventsRequest().withLogEvents(toInputLogEvent(collection)).withLogGroupName(logGroupName).withLogStreamName(logStreamName);
    if (StringUtils.isNotBlank(sequenceToken)) {
        req.setSequenceToken(sequenceToken);
    }
    try {
        sequenceToken = pushLogEvents(req, false, client);
        addSuccessCount(collection.size());
        ret = true;
    } catch (Throwable e) {
        addFailedCount(collection.size());
        LOG.error("Failed to send audit events", e);
    }
    return ret;
}
Also used : AWSLogs(com.amazonaws.services.logs.AWSLogs) PutLogEventsRequest(com.amazonaws.services.logs.model.PutLogEventsRequest)

Example 3 with AWSLogs

use of com.amazonaws.services.logs.AWSLogs in project ranger by apache.

the class AmazonCloudWatchAuditDestination method createLogStream.

private void createLogStream() {
    AWSLogs client = getClient();
    CreateLogStreamRequest req = new CreateLogStreamRequest().withLogGroupName(logGroupName).withLogStreamName(logStreamName);
    LOG.info(String.format("Creating Log Stream `%s` in Log Group `%s`", logStreamName, logGroupName));
    client.createLogStream(req);
}
Also used : CreateLogStreamRequest(com.amazonaws.services.logs.model.CreateLogStreamRequest) AWSLogs(com.amazonaws.services.logs.AWSLogs)

Example 4 with AWSLogs

use of com.amazonaws.services.logs.AWSLogs in project minijax by minijax.

the class CloudWatchAppenderTest method testAlreadyExists.

@Test
public void testAlreadyExists() {
    final Context mockContext = mock(Context.class);
    final PutLogEventsResult mockResult = mock(PutLogEventsResult.class);
    when(mockResult.getNextSequenceToken()).thenReturn("2");
    final AWSLogs mockAwsLogs = mock(AWSLogs.class);
    when(mockAwsLogs.createLogGroup(any())).thenThrow(ResourceAlreadyExistsException.class);
    when(mockAwsLogs.createLogStream(any())).thenThrow(ResourceAlreadyExistsException.class);
    when(mockAwsLogs.putLogEvents(any())).thenReturn(mockResult);
    final CloudWatchAppender appender = new CloudWatchAppender();
    appender.setContext(mockContext);
    appender.setAwsLogs(mockAwsLogs);
    appender.start();
    appender.doAppend(new LoggingEvent());
    appender.stop();
}
Also used : Context(ch.qos.logback.core.Context) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) AWSLogs(com.amazonaws.services.logs.AWSLogs) Test(org.junit.Test)

Example 5 with AWSLogs

use of com.amazonaws.services.logs.AWSLogs in project cloudwatch-logback-appender by j256.

the class CloudWatchAppenderTest method testBigMessageDrop.

@Test(timeout = 10000)
public void testBigMessageDrop() throws InterruptedException {
    CloudWatchAppender appender = new CloudWatchAppender();
    AWSLogs awsLogClient = createMock(AWSLogsClient.class);
    appender.setAwsLogsClient(awsLogClient);
    appender.setMaxBatchSize(1);
    appender.setRegion("region");
    final String logGroup = "pfqoejpfqe";
    appender.setLogGroup(logGroup);
    final String logStream = "pffqjfqjpoqoejpfqe";
    appender.setLogStream(logStream);
    appender.setContext(LOGGER_CONTEXT);
    PatternLayout layout = new PatternLayout();
    layout.setContext(LOGGER_CONTEXT);
    layout.setPattern("[%thread] %level %logger{20} - %msg%n%xThrowable");
    layout.start();
    appender.setLayout(layout);
    EmergencyAppender emergency = new EmergencyAppender();
    appender.addAppender(emergency);
    final String loggerName = "name";
    final Level level = Level.DEBUG;
    String message = "ytjkuyliuyiuk";
    LoggingEvent event = createEvent(loggerName, level, message, System.currentTimeMillis());
    int maxSize = 10;
    appender.setMaxEventMessageSize(maxSize);
    appender.setTruncateEventMessages(false);
    final PutLogEventsResult result = new PutLogEventsResult();
    String sequence = "ewopjfewfj";
    result.setNextSequenceToken(sequence);
    awsLogClient.shutdown();
    // =====================================
    replay(awsLogClient);
    appender.start();
    // for coverage
    appender.start();
    appender.append(event);
    while (emergency.event == null) {
        Thread.sleep(100);
    }
    appender.stop();
    verify(awsLogClient);
    assertSame(event, emergency.event);
    assertEquals(0, appender.getEventsWrittenCount());
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) PatternLayout(ch.qos.logback.classic.PatternLayout) PutLogEventsResult(com.amazonaws.services.logs.model.PutLogEventsResult) Level(ch.qos.logback.classic.Level) AWSLogs(com.amazonaws.services.logs.AWSLogs) Test(org.junit.Test)

Aggregations

AWSLogs (com.amazonaws.services.logs.AWSLogs)17 Test (org.junit.Test)10 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)9 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)8 PutLogEventsResult (com.amazonaws.services.logs.model.PutLogEventsResult)8 PatternLayout (ch.qos.logback.classic.PatternLayout)7 Level (ch.qos.logback.classic.Level)6 PutLogEventsRequest (com.amazonaws.services.logs.model.PutLogEventsRequest)6 IAnswer (org.easymock.IAnswer)5 InputLogEvent (com.amazonaws.services.logs.model.InputLogEvent)3 Context (ch.qos.logback.core.Context)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 CreateLogStreamRequest (com.amazonaws.services.logs.model.CreateLogStreamRequest)2 DescribeLogGroupsRequest (com.amazonaws.services.logs.model.DescribeLogGroupsRequest)2 DescribeLogGroupsResult (com.amazonaws.services.logs.model.DescribeLogGroupsResult)2 DescribeLogStreamsRequest (com.amazonaws.services.logs.model.DescribeLogStreamsRequest)2 DescribeLogStreamsResult (com.amazonaws.services.logs.model.DescribeLogStreamsResult)2 Appender (ch.qos.logback.core.Appender)1 AWSLogsClientBuilder (com.amazonaws.services.logs.AWSLogsClientBuilder)1 CreateLogGroupRequest (com.amazonaws.services.logs.model.CreateLogGroupRequest)1