Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable debug logging through OSLog

Tags:

ios

oslog

I've set up logging through OSLog in my iOS App, and I've added a button to export logs with OSLogStore:

extension OSLog {
    private static let subsystem = Bundle.main.bundleIdentifier!
   
    @available(iOS 15.0, *)
    static func getLogs(inLast: TimeInterval) -> [OSLogEntryLog]? {
        guard let store = try? OSLogStore(scope: .currentProcessIdentifier) else { return nil }
        let startTime = store.position(timeIntervalSinceEnd: -inLast)
        guard let entries = try? store.getEntries(at: startTime).compactMap({ $0 as? OSLogEntryLog }).filter({ $0.subsystem == subsystem }) else { return nil }
        return entries
    }
}

This works fine when testing locally, all logs are exported even in release builds. However, when our team is testing builds through TestFlight, debug logs are not exported. Is there a way to export all logs including debug logs?

like image 702
Nick Avatar asked Aug 04 '26 20:08

Nick


1 Answers

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>

Also, you might want to add

<key>Enable-Private-Data</key>
<true/>

to the same dictionary to not have all your parameters redacted.

And finally, don't use .filter on the getEntries() result, it's extremely slow. Instead use the matching overload with a NSPredicate, e.g.

try? store
    .getEntries(
        at: startTime, 
        matching: NSPredicate(format: "subsystem == \"\(subsystem)\"")
    )
    .compactMap({ $0 as? OSLogEntryLog })
like image 86
Claus Jørgensen Avatar answered Aug 07 '26 09:08

Claus Jørgensen