AWS SDK 또는 CLI와 DescribeLogStreams 함께 사용 - Amazon CloudWatch Logs

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK 또는 CLI와 DescribeLogStreams 함께 사용

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

CLI
AWS CLI

다음 명령은 로그 그룹 my-logs2015 접두사로 시작하는 모든 로그 스트림을 보여 줍니다.

aws logs describe-log-streams --log-group-name my-logs --log-stream-name-prefix 2015

출력:

{ "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 } ] }
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 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 세부 정보는 API 참조의 DescribeLogStreamsAWS SDK for Java 2.x 를 참조하세요.

AWS SDK 개발자 안내서 및 코드 예제의 전체 목록은 섹션을 참조하세요AWS SDK에서 CloudWatch Logs 사용. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.