0

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!

szagun
  • 123
  • 8

1 Answers1

1

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.

szagun
  • 123
  • 8
  • 1
    Hey @szagun, i have a question. Did you figure out how to use the ".position(timeIntervalSinceEnd:)"? Its always returning the entire logs. What is seconds in your case? – GeorgeJ Aug 04 '23 at 09:01
  • I am using it like this let position = store.position(date: lastLogExportDate) where lastLogExportDate is the time for last export. – Marc T. Aug 09 '23 at 12:52