This is not a duplicate of the linked questions
print
vs NSLog
differencesprint
)Presently I am using print
statements with some global functions
OSLog is Apple's current recommended logging approach. There is much more to OSLog than we have explored here, such as signposts to monitor app performance and so it is likely we come back to this topic in another article in the future. Have you started using OSLog , if so what do you think of it?
SwiftLog is a Swift logging API package. To get started with SwiftLog, import Logging . The most important type is Logger which you can use to emit log messages.
NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id).
Logger type provides us with functions to log a message with different emergency levels. For example, the trace function works as debug print, and the system doesn't store it. The warning function allows us to log errors that are not fatal for our app, but we still need to know about them.
Take a look at os_log
. It offers all the things you're looking for.
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
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:
NSLog
is deprecated and some of the benefits of using the new os_log
library.The reason os_log
is so powerful is because:
private
and public
logsprint
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 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With