0

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
    I found an answer but written in objective C. It reopens `stderr` as a file. The same technique will work in Swift. https://stackoverflow.com/questions/3184235/how-to-redirect-the-nslog-output-to-file-instead-of-console – JeremyP Sep 22 '21 at 07:44
  • I initially closed the question, but then changed my mind because there's still some work needed to translate the code to Swift. – JeremyP Sep 22 '21 at 07:53

1 Answers1

0

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

Krzysio
  • 11
  • 3