Read Logs Using the New Swift Os_Log API

Read logs using the new swift os_log api

Looks like you need to use the enhanced Console instead of your own log viewer. The logs are compressed and not expanded until viewed - this makes logging much less intrusive at debug levels. There is no text form of the logs however.

See the 2016 WWDC video session 721 "Unified Logging and Activity Tracing" https://developer.apple.com/videos/play/wwdc2016/721/

Also the Apple sample app that demos the new approach has an undocumented build setting that I had to add to my iOS app. See the setting in the 'Paper Company (Swift)' iOS app.
The setting is found in the Targets section of the top level xCode window. These are the steps that I followed:

  1. On the Build Settings page add in "User-Defined" a new section = ASSETCATALOG_COMPRESSION.

  2. Under it add two lines:

Debug = lossless

Release = respect-asset-catalog

After adding this build setting then logging worked in my app as per the video session demo.

Viewing os_log messages in device console

The "Devices and Simulators" window only shows crash reports. Use the Console app or Terminal, via the log --stream command, to see live log output.

To see the device's live log messages via the Console app, either when running from Xcode or when running from the device directly:

  • Open Console.app.
  • Click on the device's name in the left side panel, under "Devices".
  • Select Action, then Include Info Messages from the menu. If you are also using .debug level messages, make sure to select Include Debug Messages as well. (Without those items selected, the Console displays .default, .fault, and .error level messages only.)

If you still don't see the messages, try entering this Terminal command to configure the logging levels for your app:

sudo log config --subsystem com.test.testapp --mode level:debug

This turns on .debug-level logging for subsystem "com.test.testapp" (which includes .info and .default messages).

If you want to persist the messages, rather than the default of memory-only, turn on persistence for the three levels at the same time, like so:

sudo log config --subsystem com.test.testapp --mode level:debug,persist:debug

Regardless of any log settings, though, only crash reports will appear in the "Devices and Simulators" window.

Using os_log to log function arguments, or other dynamic data

See Logging:

Formatting Log Messages

To format a log message, use a standard NSString or printf format string, ...

and String Format Specifiers for the standard format string specifiers, such as %@ and %d.

In your case:

os_log("foo: %@ %@", log: .default, type: .debug, x, y.description)

The format string is restricted to static strings to prevent
(unintentional) expansion of format string specifiers. Here is an example demonstrating the
problem, using NSLog() because that does not restrict the format
to constant strings:

let s = "50%"
NSLog("\(s)percent")
// Output: 500x0ercent

The %p expects a pointer on the variable argument list, which is
not provided. This is undefined behavior, it can lead to crashes
or unexpected output.

How can I retrieve messages logged with os_log from iPad/iPhone?

On iOS 11, you can now capture a sysdiagnose and get access to the sysdiagnose for emailing (don't email this, it's huge), or exporting to dropbox/airdrop to your Mac. Instructions.

The gist of it is :

  • hold down volume up and volumn down and power button for 1.5 seconds until the screen vibrates.
  • then wait for upto 10 minutes and on your iOS 11 device go to:

Settings > Privacy > Analytics > Analytics Data > sysdiagnose_[xxx] > tap share icon in the nav bar. For me it took less than a minute.

Search the logs for sysdiagnose. It will be something like sysdiagnose_2018.12.13_14-16-19-0500_iPhone_OS_iPhone_15G77

Then share it through AirDrop. You don't need a cable and iTunes anymore.



Related Topics



Leave a reply



Submit