Fine-grained logging

Fine-grained logging allows you to specify a logging level for a target. A target is defined by a resource type and a resource name. Currently, AWS IoT supports thing groups as targets. Fine-grained logging allows you to set a logging level for a specific thing group. Fine-grained logs are stored in Amazon CloudWatch under the log group AWSIotLogsV2.

In this exercise you will learn how to set the log level for a thing group that is different from the global log level. The global log level has been set to WARN. For a particular thing group you will set a more verbose log level. Then you will create some log entries by publishing messages with the device you used in the previous section. Then you will use different filters to search the logs.

Use the thing group building-one that you have created earlier

THING_GROUP_NAME=building-one

Get the current logging configuration for fine-grained logging

aws iot get-v2-logging-options

Set the logging level to DEBUG for the thing group

aws iot set-v2-logging-level --log-level DEBUG \
	  --log-target "{\"targetType\": \"THING_GROUP\", \"targetName\": \"$THING_GROUP_NAME\"}"

# verify the logging level
aws iot list-v2-logging-levels

Publish messages like you did in the previous section to a topic where you are allowed to publish and also to topics where you don’t have permissions to publish.

Wait some minutes because it can take a small amount of time until logs are delivered to CloudWatch.

Now do some searches in the logs for events in the last hour. You will search based on different log levels or based on the client id.

# calculate the milliseconds since the epoch minus 1h
starttime=$(($(($(date '+%s') - 3600)) * 1000))

# search all logs in the last hour without any filters
aws logs filter-log-events --log-group-name AWSIotLogsV2 \
  --start-time $starttime

# search all logs where the log level is INFO
aws logs filter-log-events --log-group-name AWSIotLogsV2 \
  --start-time $starttime --filter-pattern "{$.logLevel = INFO}"

# search all logs where the log level is ERROR
aws logs filter-log-events --log-group-name AWSIotLogsV2 \
  --start-time $starttime --filter-pattern "{$.logLevel = ERROR}"

# search for log entries from your thing used
aws logs filter-log-events --log-group-name AWSIotLogsV2 \
  --start-time $starttime --filter-pattern "{$.clientId = $THING_NAME}"