Writing Custom Messages to CloudWatch Logs - Amazon Kinesis Data Analytics

Writing Custom Messages to CloudWatch Logs

You can write custom messages to your Kinesis Data Analytics 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 repository to your application's pom.xml file:

    <repository> <id>log4j</id> <url>https://mvnrepository.com/artifact/log4j/log4j</url> </repository>
  2. Add the following dependencies to your application's pom.xml file:

    <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <scope>compile</scope> </dependency>
  3. Include the object from the library:

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

    private static final Logger log = Logger.getLogger(YourApplicationClass.class);
  5. 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.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" }

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