将自定义消息写入 CloudWatch 日志 - Managed Service for Apache Flink
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon Managed Service for Apache Flink 之前称为 Amazon Kinesis Data Analytics for Apache Flink。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将自定义消息写入 CloudWatch 日志

您可以向 Apache Flink 应用程序日志的托管服务写入自定义消息。 CloudWatch 您可以使用 Apache log4j 库或 Simple Logging Facade for Java (SLF4J) 库以执行该操作。

使用 Log4J 写入 CloudWatch 日志

  1. 将以下依赖项添加到应用程序的 pom.xml 文件中:

    <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6.1</version> </dependency>
  2. 包括库中的对象:

    import org.apache.logging.log4j.Logger;
  3. 实例化 Logger 对象并传入您的应用程序类:

    private static final Logger log = LogManager.getLogger.getLogger(YourApplicationClass.class);
  4. 使用 log.info 写入到日志。将在应用程序日志中写入大量消息。为了便于筛选自定义消息,请使用 INFO 应用程序日志级别。

    log.info("This message will be written to the application's CloudWatch log");

应用程序在日志中写入一条记录,并显示类似下面的消息:

{ "locationInformation": "com.amazonaws.services.managed-flink.StreamingJob.main(StreamingJob.java:95)", "logger": "com.amazonaws.services.managed-flink.StreamingJob", "message": "This message will be written to the application's CloudWatch log", "threadName": "Flink-DispatcherRestEndpoint-thread-2", "applicationARN": "arn:aws:kinesisanalyticsus-east-1:123456789012:application/test", "applicationVersionId": "1", "messageSchemaVersion": "1", "messageType": "INFO" }

使用 SLF4J 写入 CloudWatch 日志

  1. 将以下依赖项添加到应用程序的 pom.xml 文件中:

    <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> <scope>runtime</scope> </dependency>
  2. 包括库中的对象:

    import org.slf4j.Logger; import org.slf4j.LoggerFactory;
  3. 实例化 Logger 对象并传入您的应用程序类:

    private static final Logger log = LoggerFactory.getLogger(YourApplicationClass.class);
  4. 使用 log.info 写入到日志。将在应用程序日志中写入大量消息。为了便于筛选自定义消息,请使用 INFO 应用程序日志级别。

    log.info("This message will be written to the application's CloudWatch log");

应用程序在日志中写入一条记录,并显示类似下面的消息:

{ "locationInformation": "com.amazonaws.services.managed-flink.StreamingJob.main(StreamingJob.java:95)", "logger": "com.amazonaws.services.managed-flink.StreamingJob", "message": "This message will be written to the application's CloudWatch log", "threadName": "Flink-DispatcherRestEndpoint-thread-2", "applicationARN": "arn:aws:kinesisanalyticsus-east-1:123456789012:application/test", "applicationVersionId": "1", "messageSchemaVersion": "1", "messageType": "INFO" }