How can I make os_log write to a file on iOS?

I would like to record application log data in a file which iOS users can locate using the Files app and email to me in case of problems. Can I do this using the os_log functionality? The file path is defined as below:

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsDirectory = paths[0]
        let fileName = "\(Date()) CaptionEdit Logfile.txt"
        let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)

Replies

Can I do this using the os_log functionality?

No. The system log routes log entries to its own log store and does not support writing them directly to a file. This isn’t an accidental omission but a deliberate design choice [1].

One option here is read log entries back from the log store using the .currentProcessIdentifier scope. For this and many other system log hints and tips, see Your Friend the System Log.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Partly motivated by a desire not to burn through the flash write count on the user’s SSD, something you should keep in mind as you plan your logging strategy.