getting OSLog Logger statements on device for archived build

336 views Asked by At

Is it possible to get OSLog statements on device for a release build? If I launch the app in the simulator or on device from Xcode, the app can read and display its own log statements using the code below. But in an archived build installed on the device, nothing appears. What am I doing wrong? Or is this some sort of privacy limitation by Apple? In other StackOverflow questions, I see people asking how to turn off OSLog logging for release builds, so it sounds like this logs are not automatically omitted for those builds and should be available to me.

    public static func getLogEntries(secondsAgo: TimeInterval? = nil) throws -> [OSLogEntryLog] {
        
        let subsystem = Bundle.main.bundleIdentifier
        
        // Open the log store.
        let logStore = try OSLogStore(scope: .currentProcessIdentifier)
        
        // Get all the logs from the last so many seconds.
        let seconds: TimeInterval = (secondsAgo ?? -600) * -1
        let minutesAgo = logStore.position(date: Date().addingTimeInterval(seconds))
        
        // Fetch log objects.
        let allEntries = try logStore.getEntries(at: minutesAgo)
        
        // Filter the log to be relevant for our specific subsystem
        // and remove other elements (signposts, etc).
        return allEntries
            .compactMap { $0 as? OSLogEntryLog }
            .filter { $0.subsystem == subsystem }
    }

2

There are 2 answers

0
Claus Jørgensen On BEST ANSWER

Per discussion in the comments, if you want debug level logs you need to add this to your Info.plist

<key>OSLogPreferences</key>
<dict>
    <key>your.subsystem.key.here</key>
    <dict>
        <key>your-category-here</key>
        <dict>
            <key>Level</key>
            <dict>
                <key>Enable</key>
                <string>Debug</string>
                <key>Persist</key>
                <string>Debug</string>
            </dict>
        </dict>
    </dict>
</dict>
3
Rob Napier On

"so it sounds like this logs are not automatically omitted for those builds"

Correct. They do exist and are logged to the central logging facility. You can access them using the Console app on Mac.

and should be available to me.

No. The app cannot access the central logging facility (OSLogStore) on iOS. Hopefully some day this will be allowed, but it currently is not, and Apple has made no particular hint that they will change this in the future. Your only option is to open a Feedback to Apple (which they will file or ignore; there is no way to know), and hope it will be added some day. If you need production logs, you will need to manage them yourself outside of OSLog.