Page updated Nov 11, 2023

Listen to log events

The Amplify Logger sends errors that occur when using it through Amplify Hub. To ensure that errors do not occur when logging, log messages should be validated and follow the best security practices. Additionally, you should ensure that log messages do not exceed the Amazon CloudWatch log event size of 256 KB.

You can get logging error events by listening/subscribing to the logging events from the Amplify Hub.

1import Amplify
1// Assumes `unsubscribeToken` is declared as an instance variable in your view
2unsubscribeToken = Amplify.Hub.listen(to: .logging) { payload in
3 switch payload.eventName {
4 case HubPayload.EventName.Logging.writeLogFailure:
5 print("Error writing to local log")
6 case HubPayload.EventName.Logging.flushLogFailure:
7 print("Error sending log events to CloudWatch")
8 default:
9 break
10 }
11}
1import Amplify
1// Assumes `sink` is declared as an instance variable in your code
2sink = Amplify.Hub
3 .publisher(for: .logging)
4 .sink { payload in
5 switch payload.eventName {
6 case HubPayload.EventName.Logging.writeLogFailure:
7 print("Error writing to local log")
8 case HubPayload.EventName.Logging.flushLogFailure:
9 print("Error sending log events to CloudWatch")
10 default:
11 break
12 }
13 }