Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 AWS
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
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 { region = "us-west-2" }.use { logs -> logs.deleteSubscriptionFilter(request) println("Successfully deleted CloudWatch logs subscription filter named $filter") } }
-
API の詳細については、「AWS SDK for Kotlin API リファレンス」の「DeleteSubscriptionFilter
」を参照してください。
-
次のコード例は、DescribeSubscriptionFilters
を使用する方法を示しています。
- SDK for Kotlin
-
注記
GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 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}") } } }
-
API の詳細については、「AWS SDK for Kotlin API リファレンス」の「DescribeSubscriptionFilters
」を参照してください。
-
次の例は、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
」を参照してください。
-