Search code examples
iosswiftloggingnslog

iOS - save to log file and extract later


So i am trying to save a log file and then open it in my office.

I have tried "Willow" and "CocoaLumberjack" yet i fail.

I have tried the open console yet it shows logs only from the time i connected the device to the mac. Also tried:

NSLog("example")

and

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)

Am i doing something wrong or there is other way?

Thanks


Solution

  • Ok so i managed to do so with "CocoaLumberjack" pod.

    Pretty much as in the documentation.

    I have created a file like so:

    import Foundation
    import CocoaLumberjack
    
    func setupLogging() {
       DDLog.add(DDOSLogger.sharedInstance) // Uses os_log
       let fileLogger: DDFileLogger = DDFileLogger() // File Logger
       fileLogger.rollingFrequency = 60 * 60 * 24 // 24 hours
       fileLogger.logFileManager.maximumNumberOfLogFiles = 7
       DDLog.add(fileLogger)
    }
    
    func writeLog(message: String) {
       DDLogDebug(message)
    }
    

    In appDelegate i did this:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        setupLogging()
        return true
    }
    

    And then wherever i wanted to write a log i did like so:

    writeLog(message: "test")
    

    After finishing i had to extract the log like so: windows -> devices and simulator -> choose the device -> choose the app -> scroll down to the gear icon -> download container -> then show package contents -> library/caches/logs :)