如何将应用程序日志写入文件并获取它们

Off*_*set 8 logging ios swift swift3

我在iOS开发中采取了一些步骤,并在iOS中搜索使用日志记录的方法.

我发现这些关于使用swift 3进行日志记录的文档:https://developer.apple.com/documentation/os/logging#1682426

文档说日志没有保存在磁盘上.获取日志和处理文件的典型方法是什么?

Raj*_*ain 10

斯威夫特 3.0 版本

在你的项目中创建一个新的 swift 文件“TextLog.swift”

import Foundation

struct TextLog: TextOutputStream {

    /// Appends the given string to the stream.
    mutating func write(_ string: String) {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask)
        let documentDirectoryPath = paths.first!
        let log = documentDirectoryPath.appendingPathComponent("log.txt")

        do {
            let handle = try FileHandle(forWritingTo: log)
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } catch {
            print(error.localizedDescription)
            do {
                try string.data(using: .utf8)?.write(to: log)
            } catch {
                print(error.localizedDescription)
            }
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

在 AppDelegate.swift 文件底部初始化记录器

var textLog = TextLog()
Run Code Online (Sandbox Code Playgroud)

在应用程序中的任何地方使用它,如下所示

textLog.write("hello")
Run Code Online (Sandbox Code Playgroud)

  • 我在哪里可以在我们的设备中看到这个日志文件? (5认同)

use*_*734 9

把这个文件放到你的项目中

//
//  log.swift
//  logtest
//

import Foundation

struct Log: TextOutputStream {

    func write(_ string: String) {
        let fm = FileManager.default
        let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
        if let handle = try? FileHandle(forWritingTo: log) {
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } else {
            try? string.data(using: .utf8)?.write(to: log)
        }
    }
}

var logger = Log()
Run Code Online (Sandbox Code Playgroud)

如果您需要记录某些内容,只需使用打印功能即可

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        print("started:", Date(), to: &logger)
        return true
    }
Run Code Online (Sandbox Code Playgroud)

要么

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    print(#file, #function, "my own text", 1, [1,2], to: &logger)
}
Run Code Online (Sandbox Code Playgroud)

在您的应用程序的'Documents'文件夹中,您可以找到'log.txt'文件,您可以在以后查看.

在运行我的测试应用程序两次时,内容看起来像

started: 2017-06-14 09:58:58 +0000
/Users/ivo_vacek/Documents/logtest/logtest/ViewController.swift viewDidLoad() my own text 1 [1, 2]
started: 2017-06-14 09:59:15 +0000
/Users/ivo_vacek/Documents/logtest/logtest/ViewController.swift viewDidLoad() my own text 1 [1, 2] 
Run Code Online (Sandbox Code Playgroud)

如果你不喜欢'全局',则将Log定义为单线类

class Log: TextOutputStream {

    func write(_ string: String) {
        let fm = FileManager.default
        let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
        if let handle = try? FileHandle(forWritingTo: log) {
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } else {
            try? string.data(using: .utf8)?.write(to: log)
        }
    }
    static var log: Log = Log()
    private init() {} // we are sure, nobody else could create it
}
Run Code Online (Sandbox Code Playgroud)

并使用它

print("started:", Date(), to: &Log.log)
Run Code Online (Sandbox Code Playgroud)

  • 无论如何,是否可以简单地使用通过调用“os_log”创建的日志并将其压缩并通过电子邮件发送?目前我只知道 sysdiagnose 方法...... (2认同)