print("debug: \(value)")
とかではつらいのかな。
と思いつつ抽出して使ってみる。
👉 Code search results
import OSLog
private let logger = Logger(subsystem: "BackyardBirds", category: "BackyardBirdsPassStatus")
logger.log("""
Processing transaction ID \(unsafeTransaction.id) for \
\(unsafeTransaction.productID)
""")
logger.debug("""
Transaction ID \(t.id) for \(t.productID) is verified
""")
logger.error("""
Transaction ID \(t.id) for \(t.productID) is unverified: \(error)
""")
logger.info("Providing updated status to data generation")
出力レベルがあるんですね。
出力は、
2023-09-16 18:26:11.842691+0900 Develop[99999:999999] [Standard] Debug: Uhooi
2023-09-16 18:26:11.842722+0900 Develop[99999:999999] [Standard] Info: Uhooi
2023-09-16 18:26:11.842766+0900 Develop[99999:999999] [Standard] Notice: Uhooi
2023-09-16 18:26:11.842811+0900 Develop[99999:999999] [Standard] Error: Uhooi
2023-09-16 18:26:11.842858+0900 Develop[99999:999999] [Standard] Fault: Uhooi
{Timestamp} {Library}[{PID}:{TID}] [{Category}] {Message} の形式で出力されています。
👉 os.Loggerの説明と使い方(Swift) #Swift - Qiita
なるほど。
■ やってみる
import OSLog
private let logger = Logger(subsystem: "subsystem", category: "category")
// ...
static func create(modelContainer: ModelContainer) {
logger.debug("debug Creating service instance.")
logger.trace("trace Creating service instance.")
logger.info("infoCreating service instance.")
logger.notice("notice Creating service instance.")
logger.error("error Creating service instance.")
logger.warning("warning Creating service instance.")
logger.fault("fault Creating service instance.")
logger.critical("critical Creating service instance.")
shared = SoundDataService(modelContainer: modelContainer)
}
debug Creating service instance.
trace Creating service instance.
infoCreating service instance.
notice Creating service instance.
error Creating service instance.
warning Creating service instance.
fault Creating service instance.
critical Creating service instance.
あれ、タイムスタンプ的なのデフォルトで出ないのか。
subsystem とか category とかも何の意味があるのか。
print() で良くね?
なんなのこれ。
あ、これか。
extension 化しとくのが楽かもしれません。
extension OSLog {
static let ui = Logger(subsystem: "com.satoriku.OSLog", category: "ui")
static let network = Logger(subsystem: "com.satoriku.OSLog", category: "network")
static let viewCycle = Logger(subsystem: "com.satoriku.OSLog", category: "viewcycle")
}
👉 【Xcode/Swift】OSLogを使ってアプリログを出力する方法(ロギング) - iOS-Docs
import Foundation
import os
extension Logger {
private static var subsystem = Bundle.main.bundleIdentifier!
static let segue = Logger(subsystem: subsystem, category: "segue")
static let note = Logger(subsystem: subsystem, category: "note")
static let flashcard = Logger(subsystem: subsystem, category: "flashcard")
static let deck = Logger(subsystem: subsystem, category: "deck")
static let settings = Logger(subsystem: subsystem, category: "settings")
static let option = Logger(subsystem: subsystem, category: "option")
}
👉 Flashcard-Adder/Flashcard Adder/Logger.swift at bfa5f3526fba48f5c6596f92d7648e4137fa60c9 · Nonameentered/Flashcard-Adder
👉 OSLog and Unified logging as recommended by Apple - SwiftLee
👉 Logger | Apple Developer Documentation