Page updated Nov 11, 2023

Change log levels

In this section, you will learn about how to configure the logging level in your application when using the Amplify logger. This is helpful to help you determine the logging level that works best for your use cases, and which levels of errors or warnings you want to capture in Amazon Cloudwatch.

Change Default Log Level

Below is an example of setting the default log level to WARN:

Update the defaultLogLevel field under loggingConstraints.

1{
2 "awsCloudWatchLoggingPlugin": {
3 "enable": true,
4 "logGroupName": "<log-group-name>",
5 "region": "<region>",
6 "localStoreMaxSizeInMB": 1,
7 "flushIntervalInSeconds": 60,
8 "loggingConstraints": {
9 "defaultLogLevel": "WARN"
10 }
11 }
12}

The following are supported log levels:

  • ERROR
  • WARN
  • INFO
  • DEBUG
  • VERBOSE
  • NONE

Setting the log level to NONE effectively disables logging.

Provide a default log level at initialization and configuration of the AWSCloudWatchLoggingPlugin.

1do {
2 let loggingConstraints = LoggingConstraints(defaultLogLevel: .warn)
3 let loggingConfiguration = AWSCloudWatchLoggingPluginConfiguration(logGroupName: "<log-group-name>", region: "<region>", loggingConstraints: loggingConstraints)
4 let loggingPlugin = AWSCloudWatchLoggingPlugin(loggingPluginConfiguration: loggingConfiguration)
5 try Amplify.add(plugin: loggingPlugin)
6} catch {
7 assert(false, "Error initializing Amplify: \(error)")
8}

The following are supported log levels:

1LogLevel.error
2LogLevel.warn
3LogLevel.info
4LogLevel.debug
5LogLevel.verbose
6LogLevel.none

Setting the log level to LogLevel.none effectively disables logging.

Configure Log Level by Category

Each Amplify category can be configured to have its own logging level.

Below is an example of setting different logging levels for the Storage and Auth categories.

Add a categoryLogLevel section and specify each category and its log level.

1{
2 "awsCloudWatchLoggingPlugin": {
3 "enable": true,
4 "logGroupName": "<log-group-name>",
5 "region": "<region>",
6 "localStoreMaxSizeInMB": 1,
7 "flushIntervalInSeconds": 60,
8 "loggingConstraints": {
9 "defaultLogLevel": "ERROR",
10 "categoryLogLevel": {
11 "Authentication": "VERBOSE",
12 "Storage": "DEBUG"
13 }
14 }
15 }
16}

To disable logging for a specific category, set the log level to NONE.

Provide a dictionary of category and corresponding log levels at initialization and configuration of the AWSCloudWatchLoggingPlugin.

1do {
2 let categoryLogLevels: [String: LogLevel] = ["Authentication": .verbose, "Storage": .debug]
3 let loggingConstraints = LoggingConstraints(defaultLogLevel: .warn, categoryLogLevel: categoryLogLevels)
4 let loggingConfiguration = AWSCloudWatchLoggingPluginConfiguration(logGroupName: "<log-group-name>", region: "<region>", loggingConstraints: loggingConstraints)
5 let loggingPlugin = AWSCloudWatchLoggingPlugin(loggingPluginConfiguration: loggingConfiguration)
6 try Amplify.add(plugin: loggingPlugin)
7} catch {
8 assert(false, "Error initializing Amplify: \(error)")
9}

To disable logging for a specific category, set the log level to LogLevel.none.

The following are existing Amplify category names that are used by default by Amplify when automatically logging errors from the library.

  • Analytics
  • API
  • Authentication
  • DataStore
  • Geo
  • Hub
  • Logging
  • Predictions
  • PushNotifications
  • Storage