Concise guide to Unified Logging System

How to use OS or OS_Log on Apple platforms.

What is Unified logging system

This is a very nice description of what it is directly from Apple.

The unified logging system provides a comprehensive and performant API to capture telemetry across all levels of the system. This unified system centralizes the storage of log data in memory and in a data store on disk.


In essence it helps you log messages from your app and help you debug your application. It works across all apple platforms and developers and quality assurance engineers have a dedicated Console app available on every mac to help with the process.

How can this benefit me and my codebase

One of the main benefits for me is the security aspect. There are other great things to like such as performance improvements by minimising observer effect.

In this privacy focused times it’s good to remember that user data is important and should be respected. The logging system provides security by default.

  • For example the old NSLog would allow a password to be stored in clear text on a phone and can be easily exposed.

  • Keeping your codebase clean from print states and other 3rd party logging implementions.

  • In addtional it is also a great tool for helping to measure performance of your app using signpost system. There is a great WWDC 2018 video that goes into more details on that.

Here are the basics

What is a subsystem? Subsystem represents your app target. Your may have separate targets for your Development and your production app versions.

What is a category? Category represents your apps module or a specific section for example networkingauthentication or view life cycle.

It’s a good practice to structure your log message with a convenience struct.

 

Here we define some convenience presets and also a basic usage which is a default usage for which output at log level info.

This beauty of this is you can find your log message inside Console app, you can even run multiple simulators side by side to compare your results on debugging.

If you are running a simulator then select your device from the side panel.

If you are running a simulator then select your device from the side panel.

And use the search field to filter for required part such as category or subsystem defined or even just sting value itself, this helps you filter for only relevant information. You can save your predefined searches and add it to toolbar for quick retreval.

1*iVHzUN44QK-wWMV-DHhgcw.png

This can be really helpful when debugging network calls instead of putting print statements in your codebase which can be easily forgotten using this method provides you a much clear way to see what happens in your app.

You can also pass parameters to the log output message and set debug level.

os_log(“message %s”, log: Log.networking, type: .info, passedValue)

The best part these messages are secure by default. If app is running on device it will not display argument output. You can still force public by adding {public} to

os_log(“message %s.”, log: Log.networking, type: .info, passedValue)

As you might have noticed you need to use slightly un-Swifty looking String Format Specifiers and that is one of negatives. But if you provide a selection of documented string formatters in your codebase this will not cause any issues.

Other use for console is to track LayoutConstraints issues in your app as they do show up there too, just filter for LayoutConstraints category.

Let me know in the comments if you found other great uses.

Quick mention of Swift-Log. This is an open source effort by Apple to bring performant logging system to all platforms.

Swift is not just fo iOS and MacOS but also for Linux and even Windows. There are obviously server side reasons for this but this is potentially a great solution going forward even though Unified Logging System has only been around for 4 year there are clear benefits in using Swift-Log especially for those considering multi platform deployments or create app backend using frameworks like Vapor or Perfect.

Where to go from here

Here are other excellent resources on the subject.

Official Documentation
https://developer.apple.com/documentation/os/logging

WWDC 2016 Talk: Unified Logging and Activity Tracing https://developer.apple.com/videos/play/wwdc2016/721/

Migrating to Unified Logging: Console and Instrumentshttps://www.raywenderlich.com/605079-migrating-to-unified-logging-console-and-instruments

OSLog and Unified logging as recommended by Applehttps://www.avanderlee.com/debugging/oslog-unified-logging/