Page updated Nov 11, 2023

Flush logs

When using the Amplify Logger, all logged messages are saved locally on the user's device first and then flushed at a set interval that you can customize. You can also choose to flush events manually if needed by following the steps outlined in this section.

Change automatic log flush interval

You can customize the time interval for when logs are automatically flushed and sent to CloudWatch.

Below is an example of setting the time interval to 120 seconds:

Update the flushIntervalInSeconds field in the logging configuration file.

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

Provide a flushIntervalInSeconds parameter initialization and configuration of the AWSCloudWatchLoggingPlugin.

1AWSCloudWatchLoggingPluginConfiguration config = new AWSCloudWatchLoggingPluginConfiguration (<log-group-name>, <region>, 120);
2Amplify.addPlugin(new AWSCloudWatchLoggingPlugin(config));
1val config = AWSCloudWatchLoggingPluginConfiguration(logGroupName = <log-group-name>, region = <region>, flushIntervalInSeconds = 120)
2Amplify.addPlugin(AWSCloudWatchLoggingPlugin(config))
1AWSCloudWatchLoggingPluginConfiguration config = new AWSCloudWatchLoggingPluginConfiguration (<log-group-name>,<region>, 120);
2Amplify.addPlugin(new AWSCloudWatchLoggingPlugin(config));

Manually flush logs

You can choose at anytime to flush the log messages that are saved locally on the user's device, to then send them immediately to Amazon CloudWatch.

Add import statement to the AWSCloudWatchLoggingPlugin

1import com.amplifyframework.logging.cloudwatch.AWSCloudWatchLoggingPlugin;

Execute the flush log function from the plugin.

1AWSCloudWatchLoggingPlugin plugin = (AWSCloudWatchLoggingPlugin)Amplify.Logging.getPlugin("awsCloudWatchLoggingPlugin");
2plugin.flushLogs(
3 () -> {
4 // logs flushed successfully
5 }, error -> {
6 // failed to flush logs
7 }
8);

Add import statement to the AWSCloudWatchLoggingPlugin

1import com.amplifyframework.logging.cloudwatch.AWSCloudWatchLoggingPlugin

Execute the flush log function from the plugin.

1val plugin = Amplify.Logging.getPlugin("awsCloudWatchLoggingPlugin") as? AWSCloudWatchLoggingPlugin
2plugin?.flushLogs(
3 {
4 // logs flushed successfully
5 },{ error ->
6 // failed to flush logs
7 }
8);

Add import statement to the AWSCloudWatchLoggingPlugin

1import com.amplifyframework.logging.cloudwatch.AWSCloudWatchLoggingPlugin;

Execute the flush log function from the plugin.

1AWSCloudWatchLoggingPlugin plugin = (AWSCloudWatchLoggingPlugin)Amplify.Logging.getPlugin("awsCloudWatchLoggingPlugin");
2plugin.flushLogs(
3 () -> {
4 // logs flushed successfully
5 }, error -> {
6 // failed to flush logs
7 }
8);