本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
将自定义消息写入到 CloudWatch 日志
您可以将自定义消息写入到 Kinesis Data Analytics 应用程序 CloudWatch 日志。您可以使用 Apache log4j
Simple Logging Facade for Java (SLF4J)
写入 CloudWatch 使用 Log4J 日志
-
将以下依赖项添加到应用程序的
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>
-
包括库中的对象:
import org.apache.logging.log4j.Logger;
-
实例化
Logger
对象并传入您的应用程序类:private static final Logger log = Logger.getLogger(
YourApplicationClass
.class); -
使用
log.info
写入到日志。将在应用程序日志中写入大量消息。为了便于筛选自定义消息,请使用INFO
应用程序日志级别。log.info("This message will be written to the application's CloudWatch log");
应用程序在日志中写入一条记录,并显示类似下面的消息:
{ "locationInformation": "com.amazonaws.services.kinesisanalytics.StreamingJob.main(StreamingJob.java:95)", "logger": "com.amazonaws.services.kinesisanalytics.StreamingJob", "message": "This message will be written to the application's CloudWatch log", "threadName": "Flink-DispatcherRestEndpoint-thread-2", "applicationARN": "arn:aws:kinesisanalytics:us-east-1:123456789012:application/test", "applicationVersionId": "1", "messageSchemaVersion": "1", "messageType": "INFO" }
写入 CloudWatch 使用 SLF4J 的日志
-
将以下依赖项添加到应用程序的
pom.xml
文件中:<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> <scope>runtime</scope> </dependency>
-
包括库中的对象:
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
-
实例化
Logger
对象并传入您的应用程序类:private static final Logger log = LoggerFactory.getLogger(
YourApplicationClass
.class); -
使用
log.info
写入到日志。将在应用程序日志中写入大量消息。为了便于筛选自定义消息,请使用INFO
应用程序日志级别。log.info("This message will be written to the application's CloudWatch log");
应用程序在日志中写入一条记录,并显示类似下面的消息:
{ "locationInformation": "com.amazonaws.services.kinesisanalytics.StreamingJob.main(StreamingJob.java:95)", "logger": "com.amazonaws.services.kinesisanalytics.StreamingJob", "message": "This message will be written to the application's CloudWatch log", "threadName": "Flink-DispatcherRestEndpoint-thread-2", "applicationARN": "arn:aws:kinesisanalytics:us-east-1:123456789012:application/test", "applicationVersionId": "1", "messageSchemaVersion": "1", "messageType": "INFO" }