xcode Apple 推荐的 Swift 日志记录方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50728431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apple Recommended Logging approach for Swift
提问by user1046037
Note:
笔记:
This is not a duplicate of the linked questions
这不是链接问题的重复
Goal:
目标:
- I am not looking for a
print
vsNSLog
differences - In fact I don't want to use either of them (presently using
print
) - I am looking for an Apple recommended way, just can't seem to find the command / documentation, I just know it exists.
- 我不是找一个
print
VSNSLog
差异 - 事实上,我不想使用它们中的任何一个(目前正在使用
print
) - 我正在寻找 Apple 推荐的方式,只是似乎找不到命令/文档,我只知道它存在。
Present implementation:
目前的实现:
Presently I am using print
statements with some global functions
目前我正在使用print
带有一些全局函数的语句
Question
题
- What is the recommended way / approach to handle errors (I don't want to use NSLog as they would write into the device's console)
- This is only for debugging purposes during development
- 处理错误的推荐方式/方法是什么(我不想使用 NSLog,因为它们会写入设备的控制台)
- 这仅用于开发期间的调试目的
回答by Honey
Take a look at os_log
. It offers all the things you're looking for.
看看os_log
。它提供了您正在寻找的所有东西。
Disclaimer:
免责声明:
I highly recommend you see this thread from Swift forums. tl;dr
我强烈建议您从 Swift 论坛中查看此主题。tl;博士
Even though it's' Apple's recommendation, its usage is debated due concerns about retrieving logs:
尽管这是 Apple 的推荐,但由于对检索日志的担忧,它的使用仍存在争议:
- retrieving logs is not a trivial process. It's actually difficult. See here
- For most users the log file can be 100-300 Mbs. Which makes it hard to send.
- 检索日志不是一个简单的过程。其实很难。看这里
- 对于大多数用户,日志文件可以是 100-300 Mbs。这使得发送变得困难。
It's great for debugging during development, but laborious to trigger, retrieve, send by your app users.
它非常适合在开发过程中进行调试,但触发、检索、发送应用程序用户很费力。
Example:
例子:
let customLog = OSLog(subsystem: "com.your_company.your_subsystem_name", category: "Category")
os_log("This is info that may be helpful during development or debugging.", log: customLog, type: .debug)
Some great references:
一些很好的参考:
- WWDC 2016 Unified Logging and Tracing.
- This answerby Rob. He discusses that
NSLog
is deprecated and some of the benefits of using the newos_log
library. - You can also get the logs from using the approach mentioned here. Make sure you see ?answers.
- WWDC 2016 统一日志记录和跟踪。
- 罗布的这个答案。他讨论了
NSLog
已弃用的内容以及使用新os_log
库的一些好处 。 - 您还可以使用此处提到的方法获取日志。确定你看到了吗?答案。
The reason os_log
is so powerful is because:
之所以os_log
如此强大,是因为:
- offers different log levels
- has different categories
private
andpublic
logs- it's lightweight and built by Apple. Doesn't require pods
- unlike
print
which is only available during debugging,os_log
can be used to peek into a released app (in realtime) and view the logs in the console app.
- 提供不同的日志级别
- 有不同的类别
private
和public
日志- 它是轻量级的,由 Apple 制造。不需要豆荚
- 与
print
仅在调试期间可用的不同,os_log
可用于查看已发布的应用程序(实时)并在控制台应用程序中查看日志。
This is great for observing application life cycle changes free of the greedy Xcode. Xcode will not allow the app to be put in a suspended state...
这对于在没有贪婪的 Xcode 的情况下观察应用程序生命周期变化非常有用。Xcode 不允许将应用程序置于挂起状态...
Note:os_log
is only available to +iOS10
注意:os_log
仅适用于+iOS10
There are new videos as well from WWDC 2018 and 2019, but have a higher focus on os_signpost
. See:
还有来自 WWDC 2018 和 2019 的新视频,但更关注os_signpost
. 看: