iOS Swift : routing NSLog calls to file

189 views Asked by At

is it possible to route all the NSLog calls from our iOS app and private frameworks into a single file easily.

Like redefining NSLog to something else ?

1

There are 1 answers

0
Krzysio On

I translated this answer to Swift for you. Call this function in your didFinishLaunchingWithOptions in AppDelegate.

func redirectConsoleLogToDocumentFolder() {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
        let documentsDirectory = paths[0]
        let logPath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("console.log").path
        freopen(logPath, "a+", stderr)
    }

This is the path to the console.log file on iOS devices: /var/mobile/Containers/Data/Application/<your-app>/Documents/console.log