Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Note:
This is not a duplicate of the linked questions
Goal:
- I am not looking for a
print
vs NSLog
differences
- In fact I don't want to use either of them (presently using
print
)
- I am looking for an Apple recommended way, just can't seem to find the command / documentation, I just know it exists.
Present implementation:
Presently I am using print
statements with some global functions
Question
- 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)
- This is only for debugging purposes during development
Take a look at os_log
. It offers all the things you're looking for.
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 concerns about retrieving logs:
- retrieving logs is not a trivial process. It's actually difficult. See here
- For most users the log file can be 100-300 Mbs. Which makes it hard to send.
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:
- WWDC 2016 Unified Logging and Tracing.
- This answer by Rob. He discusses that
NSLog
is deprecated and some of the benefits of using the new os_log
library.
- You can also get the logs from using the approach mentioned here. Make sure you see ? answers.
The reason os_log
is so powerful is because:
- offers different log levels
- has different categories
private
and public
logs
- it's lightweight and built by Apple. Doesn't require pods
- unlike
print
which is only available during debugging, os_log
can be used to peek into a released app (in realtime) and view the logs in the console app.
This is great for observing application life cycle changes free of the greedy Xcode. Xcode will not 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:
- WWDC 2018 - Measuring Performance Using Logging
- WWDC 2019 - Getting Started with Instruments
- WWDC 2019 - Developing a Great Profiling Experience