(Swift 5.3, Xcode 12, iOS 14 — No need for a third party service or library)
In short: You can replace your NSLog
calls with calls to Logger
.
You need to create a Logger
object (somewhere, your preference). If you want, you can make your logging easier to filter, e.g. in the Console app, by making various loggers for different parts/functions in your app.
import os.log
let downloadLogger = Logger(subsystem: "My app", category: "Downloading")
let somethingLogger = Logger(subsystem: "My app", category: "Lorem ipsum")
Then you call your logger like this:
// Some error occurs, so we log it:
downloadLogger.error("Error downloading feed contents: (error, privacy: .public)")
// Some less important log:
somethingLogger.info("Secret has been stored: (mySecret, privacy: .private(mask: .hash))")
N.B. our secret is kept secret by applying .private(mask: .hash)
.
To view and filter your logs, on the Devices screen, below the View Device Logs you'll find Open Console.
As the article source states:
"If you’d like to gather logs from a device, even when your app is not
running anymore, you can use the log
command on your Mac with the
collect
option
…
The logarchive
file that you get can then be opened in the Console app and filtered just like live logs can."
sudo log collect --device --start "2020-06-25 16:10:00" --output myapp.logarchive
(Credits: source)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…