Write custom messages to CloudWatch Logs - Managed Service for Apache Flink

Amazon Managed Service for Apache Flink was previously known as Amazon Kinesis Data Analytics for Apache Flink.

Write custom messages to CloudWatch Logs

You can write custom messages to your Managed Service for Apache Flink application's CloudWatch log. You do this by using the Apache log4j library or the Simple Logging Facade for Java (SLF4J) library.

Write to CloudWatch logs using Log4J

  1. Add the following dependencies to your application's pom.xml file:

    <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. Include the object from the library:

    import org.apache.logging.log4j.Logger;
  3. Instantiate the Logger object, passing in your application class:

    private static final Logger log = LogManager.getLogger.getLogger(YourApplicationClass.class);
  4. Write to the log using log.info. A large number of messages are written to the application log. To make your custom messages easier to filter, use the INFO application log level.

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

The application writes a record to the log with a message similar to the following:

{ "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" }

Write to CloudWatch logs using SLF4J

  1. Add the following dependency to your application's pom.xml file:

    <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> <scope>runtime</scope> </dependency>
  2. Include the objects from the library:

    import org.slf4j.Logger; import org.slf4j.LoggerFactory;
  3. Instantiate the Logger object, passing in your application class:

    private static final Logger log = LoggerFactory.getLogger(YourApplicationClass.class);
  4. Write to the log using log.info. A large number of messages are written to the application log. To make your custom messages easier to filter, use the INFO application log level.

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

The application writes a record to the log with a message similar to the following:

{ "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" }