Overview:
- I am logging some messages using
Logger
- I would like these messages to be printed on to Xcode without the timestamp and other details
- I want only the message to be displayed
Questions:
- How can I display only the message on the Xcode console (see preferred output)?
- Is there a setting in Xcode to remove the timestamp prefix?
- Is there an alternate approach?
Note:
- I need to use
Logger
because this app would be released, so print is not an option.
Example Code
import SwiftUI
import os
struct ContentView: View {
let logger = Logger(subsystem: "MyApp", category: "MyCategory")
var body: some View {
Text("Hello, world!")
.onAppear {
logger.debug("content view displayed")
}
}
}
Actual Output:
2022-11-25 18:41:10.116408+0800 Demo[36175:5518724] [MyCategory] content view displayed
Preferred Output:
One of the following would be ideal:
content view displayed
Demo[36175:5518724] [MyCategory] content view displayed
My Failed Attempt
I event tried to use print in debug mode, but couldn't make it compile:
Following is my failed attempt:
import os
#if DEBUG
struct Logger {
let subsystem: String
let category: String
func debug(_ message: String) {
print(message)
}
//This will not help:
// func debug(_ message: OSLogMessage) {
// print(message)
// }
}
#endif
For general advice on this subject, see Your Friend the System Log. The limitation mentioned there (r. 32863680) means you have to choose between using the system log and getting the exact output you want in the Xcode console pane. IMO it’s better to choose the system log, and use Console app to get your nice output, but there are differences in opinion.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"