Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Logs to CloudWatch from a iOS App?

I have a iOS App and want to log some things for example when an error happened. Is there a possibility to send those Logs to Cloudwatch?

Thank you.

like image 921
Dr. Marc Avatar asked Jan 27 '23 02:01

Dr. Marc


1 Answers

You can use AWSLogs SDK to send logs from an iOS app to CloudWatch. Add the following line to your Podfile under the app target section to consume the SDK via cocoapods:

pod 'AWSLogs', '~> 2.7'

Documentation: https://github.com/aws-amplify/aws-sdk-ios/tree/master/AWSLogs Source: https://github.com/aws-amplify/aws-sdk-ios/tree/master/AWSLogs

To instantiate the client, do the following:

let logs = AWSLogs.default()

Once you have the logs client created, you need to create a log group and log stream. You can do this via Amazon CloudWatch Logs console or through the SDK. If you want to create using the SDK, do the following:

Create a log group:

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/createLogGroup:

Create a log stream:

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/createLogStream:

Now, you can start sending the logs to the log stream. You need a sequence token which you can obtain by doing a DescribeLogStreams call. See https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/DescribeLogStreams:

After this you can call putLogEvents in order to send the logs:

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/putLogEvents:

You can take a look at our tests for an example: https://github.com/aws-amplify/aws-sdk-ios/blob/master/AWSLogsUnitTests/AWSGeneralLogsTests.m#L1247

like image 157
Karthikeyan Avatar answered Feb 03 '23 18:02

Karthikeyan