기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
AWS SDK 또는 CLI와 GetLogEvents 함께 사용
다음 코드 예시는 GetLogEvents의 사용 방법을 보여 줍니다.
- CLI
-
- AWS CLI
-
다음 명령은
my-logs로그 그룹에서 이름이20150601인 로그 스트림의 로그 이벤트를 검색합니다.aws logs get-log-events --log-group-namemy-logs--log-stream-name20150601출력:
{ "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.DescribeLogStreamsRequest; import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; import java.time.Instant; import java.time.temporal.ChronoUnit; /** * 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 = "WeathertopJavaContainerLogs" ; //args[0]; String logStreamName = "weathertop-java-stream" ; //args[1]; Region region = Region.US_EAST_1 ; CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() .region(region) .build(); getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); cloudWatchLogsClient.close(); } public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, String logStreamPrefix) { try { // First, find the exact log stream name DescribeLogStreamsRequest describeRequest = DescribeLogStreamsRequest.builder() .logGroupName(logGroupName) .logStreamNamePrefix(logStreamPrefix) .limit(1) // get the first matching stream .build(); DescribeLogStreamsResponse describeResponse = cloudWatchLogsClient.describeLogStreams(describeRequest); if (describeResponse.logStreams().isEmpty()) { System.out.println("No matching log streams found for prefix: " + logStreamPrefix); return; } String exactLogStreamName = describeResponse.logStreams().get(0).logStreamName(); System.out.println("Using exact log stream: " + exactLogStreamName); long startTime = Instant.now().minus(7, ChronoUnit.DAYS).toEpochMilli(); long endTime = Instant.now().toEpochMilli(); GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() .logGroupName(logGroupName) .logStreamName(exactLogStreamName) // <-- exact name, not prefix .startTime(startTime) .endTime(endTime) .startFromHead(true) .build(); GetLogEventsResponse response = cloudWatchLogsClient.getLogEvents(getLogEventsRequest); if (response.events().isEmpty()) { System.out.println("No log events found in the past 7 days."); } else { response.events().forEach(e -> System.out.println(e.message())); } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }-
API 세부 정보는 API 참조의 GetLogEventsAWS SDK for Java 2.x 를 참조하세요.
-
AWS SDK 개발자 안내서 및 코드 예제의 전체 목록은 섹션을 참조하세요AWS SDK에서 CloudWatch Logs 사용. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.
DescribeSubscriptionFilters
GetQueryResults