【问题标题】:iOS os.log from an app deployed on device来自部署在设备上的应用程序的 iOS os.log
【发布时间】:2018-10-31 07:48:17
【问题描述】:

我正在使用新的os.log 来登录我的应用程序,如下所示:

import os.log

class ViewController: UIViewController {
    // a bunch of methods
}

extension OSLog {
    static let ui = OSLog(subsystem: "io.my.app", category: "ui")
}

extension ViewController {        
    func upload(locations: [LocationProtocol]) {
        os_log("Sending locations to server", log: .ui, type: .debug)
        self.server_api.send(locations)
    }
}

当我从 Xcode 调试应用程序时,日志显示在 Console.app 中。但是有可能以某种方式从设备上部署的应用程序实例中检索记录的字符串吗?我正在“现场”测试我的应用程序,远离笔记本电脑,并希望将收集的日志转储到文本文件中。

我是否需要以某种方式配置记录器以持久存储日志,或者只能从已部署的应用程序中获取崩溃报告?

注意:我正在使用 Swift 4 / Xcode 9+

【问题讨论】:

  • 根据网上的信息,os_log的日志存放在设备的/var/db/diagnostics/中,以后可以从那里取回并打开控制台。但我自己没有尝试过。
  • @matt 但是这家伙似乎创建了一个自定义解决方案来手动将日志写入特定文件。我想改用os.log =)
  • @matt 很有趣,应用的容器中没有 /var/db/diagnostics/ 路径。
  • 你有没有想过这个问题?

标签: ios swift xcode logging


【解决方案1】:

我相信你可以用这个答案解决这个问题:https://stackoverflow.com/a/13303081/2136375

总结一下:

步骤 1

将以下代码添加到didFinishLaunchingWithOptions 以使应用能够登录到文件:

var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let deviceName = UIDevice.current.name
let fileName = "\(deviceName) - \(Date()).log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName) freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)

第二步

info.plist添加:

替代 1

作为属性列表项:

“应用程序支持iTunes文件共享”并设置为YES。

替代 2

作为源代码:

<key>UIFileSharingEnabled</key>
<true/>

【讨论】:

  • 这很好用。只需导入 os,然后使用 os_log。下载应用程序的容器,或使用 iTunes 文件共享。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多