使用新的 swift os_log api 读取日志

Read logs using the new swift os_log api

在 iOS 10.0 中已弃用:os_log(3) 已替换 asl(3)

因此 iOS 10.0 显然弃用了 asl(Apple 系统日志)api 并将其替换为非常有限的 os_log api.

我使用类似于下面的代码片段的内容来读取 运行 应用程序的日志写入以显示在应用程序的 uitextview 中 - 现在它充满了弃用警告。有谁知道使用新的 os_log api 读取打印日志的方法吗?因为我只看到一个api写(https://developer.apple.com/reference/os/1891852-logging).

import asl

let query = asl_new(UInt32(ASL_TYPE_QUERY))
let response = asl_search(nil, query)
while let message = asl_next(response) {
    var i: UInt32 = 0
    let key = asl_key(message, i)
    print(asl_get(message, key))
    ...
}

在@Will Loew-Blosser 的回答后编辑

https://developer.apple.com/videos/play/wwdc2016/721/ 很好地解释了将来日志记录会发生什么。最大的好处是日志以某种压缩格式放置,并且只能通过新的控制台应用程序进行扩展。这几乎让我的任务毫无希望。

视频中的那个人 (Steve Szymanski) 提到了 "All ASL logging APIs are superseeded by new APIs" 和 "New APIs for searching new log data will not be made public this release" 又名 asl_search。而这正是我要找的!

他还提到 swift API 我来了。

看来您需要使用增强的控制台而不是您自己的日志查看器。日志被压缩并且在查看之前不会展开 - 这使得日志记录在调试级别的侵入性大大降低。但是没有文本形式的日志。

观看 2016 年 WWDC 视频 session 721 "Unified Logging and Activity Tracing" https://developer.apple.com/videos/play/wwdc2016/721/

此外,演示新方法的 Apple 示例应用程序还有一个未记录的构建设置,我必须将其添加到我的 iOS 应用程序中。请参阅 'Paper Company (Swift)' iOS 应用程序中的设置。 该设置位于顶层 xCode window 的“目标”部分。这些是我遵循的步骤:

  1. 在构建设置页面上添加 "User-Defined" 新部分 = ASSETCATALOG_COMPRESSION。

  2. 下面加两行:

调试 = 无损

发布=respect-asset-catalog

添加此构建设置后,根据视频 session 演示,日志记录在我的应用程序中工作。