iosswiftxcodeerror-loggingoslog

Apple Recommended Logging approach for Swift


Note:

This is not a duplicate of the linked questions

Goal:

Present implementation:

Presently I am using print statements with some global functions

Question

  1. What is the recommended way / approach to handle errors (I don't want to use NSLog as they would write into the device's console)
  2. This is only for debugging purposes during development

Solution

  • Take a look at os_log. It offers all the things you're looking for.

    NEW STUFF

    Also the more easier to use Logger API available since iOS14. See here as well

    Since iOS15 you can also retrieve the logs using OSLogStore . See here.

    Also see Acquiring Crash Reports and Diagnostic Logs


    Disclaimer:

    I highly recommend you see this thread from Swift forums. tl;dr

    Even though it's' Apple's recommendation, its usage is debated due to concerns about retrieving logs:

    It's great for debugging during development, but laborious to trigger, retrieve, send by your app users.


    Example:

    let customLog = OSLog(subsystem: "com.your_company.your_subsystem_name", category: "Category")
    os_log("This is info that may be helpful during development or debugging.", log: customLog, type: .debug)
    

    Some great references:

    The reason os_log is so powerful is because:

    enter image description here

    This is great for observing application life cycle changes free of the greedy Xcode i.e. if you’re debugging while connected to Xcode , then Xcode doesn’t allow the app to be put in a suspended state...


    Note: os_log is only available to +iOS10

    There are new videos as well from WWDC 2018 and 2019, but have a higher focus on os_signpost. See: