简体   繁体   English

如何从iOS应用程序发送日志到CloudWatch?

[英]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. 我有一个iOS应用程序,并希望记录一些事情,例如发生错误时。 Is there a possibility to send those Logs to Cloudwatch? 是否有可能将这些日志发送到Cloudwatch?

Thank you. 谢谢。

You can use AWSLogs SDK to send logs from an iOS app to CloudWatch. 您可以使用AWSLogs SDK将日志从iOS应用程序发送到CloudWatch。 Add the following line to your Podfile under the app target section to consume the SDK via cocoapods: 将以下行添加到应用程序目标部分下的Podfile中,以通过cocoapods使用SDK:

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 文档: https//github.com/aws-amplify/aws-sdk-ios/tree/master/AWSLogs来源: 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. 您可以通过Amazon CloudWatch Logs控制台或SDK执行此操作。 If you want to create using the SDK, do the following: 如果要使用SDK创建,请执行以下操作:

Create a log group: 创建日志组:

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/createLogGroup : 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 : 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. 您需要一个序列令牌,您可以通过执行DescribeLogStreams调用获得该令牌。 See https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/DescribeLogStreams : 请参阅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: 在此之后,您可以调用putLogEvents以发送日志:

https://aws-amplify.github.io/aws-sdk-ios/docs/reference/Classes/AWSLogs.html#//api/name/putLogEvents : 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 您可以查看我们的测试示例: https//github.com/aws-amplify/aws-sdk-ios/blob/master/AWSLogsUnitTests/AWSGeneralLogsTests.m#L1247

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM