Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

ios - Only Crash logs showing in Device Console

Using Xcode 12, and NSLog to log my messages. When I open the "Window -> Devices and Simulators" and I view the log files of my App on my iPhone, I only see Crash logs (crash logs of my and others' apps). Is there a way to see just my regular logging messages? NSLog output is showing in the console, no problem. Thanks....

question from:https://stackoverflow.com/questions/65829119/only-crash-logs-showing-in-device-console

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...