loggingapple-watchwatchososlog

OSLogStore on watchOS doesn't return entries


Hello.

I'm developing a tightly coupled watchOS/iOS app. The customer needs to be able to write logs to text files, from both apps, in a hassle-free way.

So I want to retrieve entries using OSLogStore, which works just fine on iOS:

let store = try OSLogStore(scope: .currentProcessIdentifier)
let pos = store.position(timeIntervalSinceEnd: -seconds)
let entries = try store
   .getEntries(with: [], at: pos, matching: nil)
   .compactMap { $0 as? OSLogEntryLog }
   .filter { $0.subsystem == Bundle.main.bundleIdentifier! }
   .map { "[\($0.date.formatted())] [\($0.category)] \($0.composedMessage)" }
   .joined(separator: "\n")

I've tried the same code on watchOS, with and without the filtering, but it always returns 0 entries.

I can't find anything related to this behavior in the documentation. So... does it just not work perhaps, or did I do something wrong?

Using a Watch SE 44mm and running watchOS 8.5

Thanks!


Solution

  • I figured it out.

    To everyone with the same problem: turns out logging is disabled by default. You can still use the os-log-API and your messages will show in the console while debugging, it'll just never write those entries to the log store.

    You'd have to install the sysdiagnose-profile on your Watch first, every 3 days if you want to keep it active.