AWS SDK 또는 CLI와 GetLogEvents 함께 사용 - AWS SDK 코드 예제

Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS

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

AWS SDK 또는 CLI와 GetLogEvents 함께 사용

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

CLI
AWS CLI

다음 명령은 my-logs 로그 그룹에서 이름이 20150601인 로그 스트림의 로그 이벤트를 검색합니다.

aws logs get-log-events --log-group-name my-logs --log-stream-name 20150601

출력:

{ "nextForwardToken": "f/31961209122447488583055879464742346735121166569214640130", "events": [ { "ingestionTime": 1433190494190, "timestamp": 1433190184356, "message": "Example Event 1" }, { "ingestionTime": 1433190516679, "timestamp": 1433190184356, "message": "Example Event 1" }, { "ingestionTime": 1433190494190, "timestamp": 1433190184358, "message": "Example Event 2" } ], "nextBackwardToken": "b/31961209122358285602261756944988674324553373268216709120" }
  • API 세부 정보는 AWS CLI 명령 참조GetLogEvents를 참조하세요.

Java
SDK for Java 2.x
참고

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetLogEvents { public static void main(String[] args) { final String usage = """ Usage: <logGroupName> <logStreamName> Where: logGroupName - The name of the log group (for example, myloggroup). logStreamName - The name of the log stream (for example, mystream). """; if (args.length != 2) { System.out.print(usage); System.exit(1); } String logGroupName = args[0]; String logStreamName = args[1]; Region region = Region.US_WEST_2; CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() .region(region) .build(); getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); cloudWatchLogsClient.close(); } public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, String logStreamName) { try { GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() .logGroupName(logGroupName) .logStreamName(logStreamName) .startFromHead(true) .build(); int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size(); for (int c = 0; c < logLimit; c++) { System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); } System.out.println("Successfully got CloudWatch log events!"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • API 세부 정보는 API 참조의 GetLogEventsAWS SDK for Java 2.x 를 참조하세요.