2017-06-14 50 views
1

我正在iOS開發中採取嬰兒步驟並搜索在iOS中使用日誌記錄的方法。如何將應用程序日誌寫入文件並獲取它們

我發現這些文檔關於與SWIFT 3日誌:https://developer.apple.com/documentation/os/logging#1682426

文檔說,該日誌地圖無法保存在磁盤上。獲取日誌和處理文件的典型方法是什麼?

+0

我想你想得到的日誌作爲像CrashReporter文件是我嗎?在這種情況下,有幾個可以歸檔的豆莢。 [PLCrashReporter](https://www.plcrashreporter.org/)是我在其中一個項目中使用的。它將日誌文件打印到文件中,並生成將發送到特定服務器的報告。 –

+0

我想用os.log來記錄參數和運行時行爲,就像在文檔中一樣。在不久的將來,我想收集這些日誌並將它們發送到別處。但我正在尋找一種乾淨的快速代碼方法,而不是一個庫。 – Offset

回答

1

把這個文件到你的項目

// 
// 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() 

,如果你需要的東西要被記錄下來,只是使用像

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

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) 
} 

打印功能在應用程序的「 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] 

,如果你不喜歡「全局」定義爲登錄類singletone

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 
} 

,並使用它像

print("started:", Date(), to: &Log.log) 
+0

奧基耶。這是一種方法,但爲什麼我不能使用os.log框架?我的意思是它包含一個LogLevel等等。我有一個問題是什麼意思在Log.log前的'&'? – Offset

+1

@Offset OSLog僅適用於iOS 10.0+,macOS 10.12+,tvOS 10.0+,watchOS 3.0+,未知SDK 8.0+。 &作爲inout使變量成爲輸入輸出參數。使用OSLog導入操作系統到您的文件 – user3441734

+0

好的只是要清楚。 OSLog僅用於調試?我不能寫入文件或可以用作文件?我認爲它很混亂。更多的是有一個可用於所有版本的NSLog。總結一下:或者我寫自己的記錄器(如上)或者使用第三方庫? – Offset

1

Swift 3.0版本

在你的項目中創建一個新的文件迅速「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) 
      } 
     } 

    } 

} 

在AppDelegate.swift底部初始化記錄器文件

var textLog = TextLog() 

使用它的任何地方應用像下面

textLog.write("hello") 
相關問題