OSLogStore: How does Predicate work?

Number:rdar://FB8518539 Date Originated:
Status:Open Resolved:
Product:iOS/macOS Product Version:
Classification: Reproducible:
With following sample code to access OSLogStore:

func getLogEntries() -> Result<[OSLogEntryLog], Error> {
    var logMessages: [OSLogEntryLog] = []
    do {
        // FB8269189: OSLogStore does not work iOS
        let logStore = try OSLogStore(scope: .currentProcessIdentifier)
        let oneHourAgo = logStore.position(date: Date().addingTimeInterval(-3600))

        // TODO: How to format the predicate? See "PREDICATE-BASED FILTERING" in `man log`.
        // Things I tried unsuccessfully:
        // - NSPredicate(format: "subsystem == %@", "com.steipete.LoggingTest")
        // - NSPredicate(format: "subsystem == \"com.steipete.LoggingTest\"")

        // FB8518476: The Swift shims for for the entries enumerator are missing.
        let allEntries = try Array(logStore.__entriesEnumerator(position: oneHourAgo, predicate: nil))
        let osLogEntryLogObjects = allEntries.compactMap { $0 as? OSLogEntryLog }
        for entry in osLogEntryLogObjects where entry.subsystem == subsystem {
        return .success(logMessages)
    } catch {
        print("Error: \(error)")
        return .failure(error)

I was unable to write a predicate to filter the log messages. Manual filtering works. Please provide sample code or fix the predicate logic - above syntax is what I would expect to work.



here is what works for me on MacOS 11.1 and Xcode 12.3

    let startTime = Date(timeIntervalSinceNow: -60)
    let logStore = try! OSLogStore.local()
    let predicate = NSPredicate(format: "category == %@",
                                argumentArray: ["app"])
    let position = logStore.position(date: startTime)
    let entries = try! logStore.getEntries(at: position, matching: predicate)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!