翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDK または CLI DescribeLogStreamsで を使用する
次のサンプルコードは、DescribeLogStreams を使用する方法を説明しています。
- CLI
-
- AWS CLI
-
次のコマンドは、ロググループ
my-logsのプレフィックス2015で始まるすべてのログストリームを表示します。aws logs describe-log-streams --log-group-namemy-logs--log-stream-name-prefix2015出力:
{ "logStreams": [ { "creationTime": 1433189871774, "arn": "arn:aws:logs:us-west-2:0123456789012:log-group:my-logs:log-stream:20150531", "logStreamName": "20150531", "storedBytes": 0 }, { "creationTime": 1433189873898, "arn": "arn:aws:logs:us-west-2:0123456789012:log-group:my-logs:log-stream:20150601", "logStreamName": "20150601", "storedBytes": 0 } ] }-
API の詳細については、「AWS CLI コマンドリファレンス」の「DescribeLogStreams
」を参照してください。
-
- Java
-
- SDK for Java 2.x
-
注記
GitHub には、その他のリソースもあります。AWS コード例リポジトリ
で全く同じ例を見つけて、設定と実行の方法を確認してください。 指定されたプレフィックスに一致する、指定されたロググループ内のログストリームを検索します。
/** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CloudWatchLogsSearch { public static void main(String[] args) { final String usage = """ Usage: <logGroupName> <logStreamName> Where: logGroupName - The name of the log group (for example, WeathertopJavaContainerLogs). logStreamName - The name of the log stream (for example, weathertop-java-stream). pattern - the pattern to use (for example, INFO) """; if (args.length != 3) { System.out.print(usage); System.exit(1); } String logGroupName = args[0] ; String logStreamName = args[1] ; String pattern = args[2] ; CloudWatchLogsClient cwlClient = CloudWatchLogsClient.builder() .region(Region.US_EAST_1) .build(); searchLogStreamsAndFilterEvents(cwlClient, logGroupName, logStreamName, pattern); } /** * Searches for log streams with a specific prefix within a log group and filters log events based on a specified pattern. * * @param cwlClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs * @param logGroupName the name of the log group to search within * @param logStreamPrefix the prefix of the log streams to search for * @param pattern the pattern to filter log events by */ public static void searchLogStreamsAndFilterEvents(CloudWatchLogsClient cwlClient, String logGroupName, String logStreamPrefix, String pattern) { DescribeLogStreamsRequest describeLogStreamsRequest = DescribeLogStreamsRequest.builder() .logGroupName(logGroupName) .logStreamNamePrefix(logStreamPrefix) .build(); DescribeLogStreamsResponse describeLogStreamsResponse = cwlClient.describeLogStreams(describeLogStreamsRequest); List<LogStream> logStreams = describeLogStreamsResponse.logStreams(); for (LogStream logStream : logStreams) { String logStreamName = logStream.logStreamName(); System.out.println("Searching in log stream: " + logStreamName); FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder() .logGroupName(logGroupName) .logStreamNames(logStreamName) .filterPattern(pattern) .build(); FilterLogEventsResponse filterLogEventsResponse = cwlClient.filterLogEvents(filterLogEventsRequest); for (FilteredLogEvent event : filterLogEventsResponse.events()) { System.out.println(event.message()); } System.out.println("--------------------------------------------------"); // Separator for better readability } } }指定されたロググループ内の最新のログストリームに関するメタデータを出力します。
/** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CloudWatchLogQuery { public static void main(final String[] args) { final String usage = """ Usage: <logGroupName> Where: logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler). """; if (args.length != 1) { System.out.print(usage); System.exit(1); } String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0]; CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() .region(Region.US_EAST_1) .build(); describeMostRecentLogStream(logsClient, logGroupName); } /** * Describes and prints metadata about the most recent log stream in the specified log group. * * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs * @param logGroupName the name of the log group */ public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) { DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder() .logGroupName(logGroupName) .orderBy(OrderBy.LAST_EVENT_TIME) .descending(true) .limit(1) .build(); try { DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest); List<LogStream> logStreams = streamsResponse.logStreams(); if (logStreams.isEmpty()) { System.out.println("No log streams found for log group: " + logGroupName); return; } LogStream stream = logStreams.get(0); System.out.println("Most Recent Log Stream:"); System.out.println(" Name: " + stream.logStreamName()); System.out.println(" ARN: " + stream.arn()); System.out.println(" Creation Time: " + stream.creationTime()); System.out.println(" First Event Time: " + stream.firstEventTimestamp()); System.out.println(" Last Event Time: " + stream.lastEventTimestamp()); System.out.println(" Stored Bytes: " + stream.storedBytes()); System.out.println(" Upload Sequence Token: " + stream.uploadSequenceToken()); } catch (CloudWatchLogsException e) { System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage()); } } }-
API の詳細については、AWS SDK for Java 2.x 「 API リファレンス」の「DescribeLogStreams」を参照してください。
-
AWS SDK 開発者ガイドとコード例の完全なリストについては、「」を参照してくださいAWS SDK での CloudWatch Logs の使用。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。
DescribeLogGroups
DescribeSubscriptionFilters