SDK for Kotlin을 사용한 CloudWatch Logs 예제 - AWS SDK 코드 예제

AWS SDK 예제 GitHub 리포지토리에 더 많은 AWS문서 SDK 예제가 있습니다.

SDK for Kotlin을 사용한 CloudWatch Logs 예제

다음 코드 예제에서는 CloudWatch Logs와 함께 AWS SDK for Kotlin을 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

주제

작업

다음 코드 예시는 DeleteSubscriptionFilter의 사용 방법을 보여줍니다.

SDK for Kotlin
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 확인하고 설정 및 실행하는 방법을 알아보세요.

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

다음 코드 예시는 DescribeSubscriptionFilters의 사용 방법을 보여줍니다.

SDK for Kotlin
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예제 리포지토리에서 전체 예제를 확인하고 설정 및 실행하는 방법을 알아보세요.

suspend fun describeFilters(logGroup: String) { val request = DescribeSubscriptionFiltersRequest { logGroupName = logGroup limit = 1 } CloudWatchLogsClient.fromEnvironment { 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}") } } }

다음 코드 예시는 StartLiveTail의 사용 방법을 보여줍니다.

SDK for Kotlin

필수 파일을 포함합니다.

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

Live Tail 세션을 시작합니다.

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) }
  • API 세부 정보는 AWS SDK for Kotlin API 참조의 StartLiveTail을 참조하세요.