CloudWatch Logs examples using SDK for Kotlin - AWS SDK for Kotlin

CloudWatch Logs examples using SDK for Kotlin

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Kotlin with CloudWatch Logs.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use DeleteSubscriptionFilter.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun deleteSubFilter( filter: String?, logGroup: String?, ) { val request = DeleteSubscriptionFilterRequest { filterName = filter logGroupName = logGroup } CloudWatchLogsClient { region = "us-west-2" }.use { logs -> logs.deleteSubscriptionFilter(request) println("Successfully deleted CloudWatch logs subscription filter named $filter") } }

The following code example shows how to use DescribeSubscriptionFilters.

SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun describeFilters(logGroup: String) { val request = DescribeSubscriptionFiltersRequest { logGroupName = logGroup limit = 1 } CloudWatchLogsClient { region = "us-west-2" }.use { cwlClient -> val response = cwlClient.describeSubscriptionFilters(request) response.subscriptionFilters?.forEach { filter -> println("Retrieved filter with name ${filter.filterName} pattern ${filter.filterPattern} and destination ${filter.destinationArn}") } } }

The following code example shows how to use StartLiveTail.

SDK for Kotlin

Include the required files.

import aws.sdk.kotlin.services.cloudwatchlogs.CloudWatchLogsClient import aws.sdk.kotlin.services.cloudwatchlogs.model.StartLiveTailRequest import aws.sdk.kotlin.services.cloudwatchlogs.model.StartLiveTailResponseStream import kotlinx.coroutines.flow.takeWhile

Start the Live Tail session.

val client = CloudWatchLogsClient.fromEnvironment() val request = StartLiveTailRequest { logGroupIdentifiers = logGroupIdentifiersVal logStreamNames = logStreamNamesVal logEventFilterPattern = logEventFilterPatternVal } val startTime = System.currentTimeMillis() try { client.startLiveTail(request) { response -> val stream = response.responseStream if (stream != null) { /* Set a timeout to unsubcribe from the flow. This will: * 1). Close the stream * 2). Stop the Live Tail session */ stream.takeWhile { System.currentTimeMillis() - startTime < 10000 }.collect { value -> if (value is StartLiveTailResponseStream.SessionStart) { println(value.asSessionStart()) } else if (value is StartLiveTailResponseStream.SessionUpdate) { for (e in value.asSessionUpdate().sessionResults!!) { println(e) } } else { throw IllegalArgumentException("Unknown event type") } } } else { throw IllegalArgumentException("No response stream") } } } catch (e: Exception) { println("Exception occurred during StartLiveTail: $e") System.exit(1) }
  • For API details, see StartLiveTail in AWS SDK for Kotlin API reference.