iOSのデバッグに便利なライブラリらしい
導入
↑の参考リンクからPodかSPMでインストールする、↓はPodの例
pod 'Willow', '~> 5.0'
使用例
Loggerという共通のデバッグ用ファイルを作成
import Willow
final class Logger {
// MARK: - Properties
static var willowLogger: Willow.Logger = {
let logLevels: LogLevel = [.all]
let writers: [LogWriter] = [ConsoleWriter(modifiers: [EmojiModifier()])]
let logger = Willow.Logger(logLevels: logLevels, writers: writers)
return logger
}()
// MARK: - Logger
static func debug(_ message: @autoclosure @escaping () -> String) {
willowLogger.debugMessage(message)
}
static func debug(_ message: @escaping () -> String) {
willowLogger.debugMessage(message)
}
static func info(_ message: @autoclosure @escaping () -> String) {
willowLogger.infoMessage(message)
}
static func info(_ message: @escaping () -> String) {
willowLogger.infoMessage(message)
}
static func event(_ message: @autoclosure @escaping () -> String) {
willowLogger.eventMessage(message)
}
static func event(_ message: @escaping () -> String) {
willowLogger.eventMessage(message)
}
static func warn(_ message: @autoclosure @escaping () -> String) {
willowLogger.warnMessage(message)
}
static func warn(_ message: @escaping () -> String) {
willowLogger.warnMessage(message)
}
static func error(_ message: @autoclosure @escaping () -> String) {
willowLogger.errorMessage(message)
}
static func error(_ message: @escaping () -> String) {
willowLogger.errorMessage(message)
}
}
// MARK: - Emoji Modifier
private final class EmojiModifier: Willow.LogModifier {
func modifyMessage(_ message: String, with logLevel: LogLevel) -> String {
let prefix: String
switch logLevel.rawValue {
case LogLevel.debug.rawValue:
prefix = "🤖[DEBUG]🤖"
case LogLevel.info.rawValue:
prefix = "🗒️[INFO]🗒️"
case LogLevel.event.rawValue:
prefix = "🎆[EVENT]🎆"
case LogLevel.warn.rawValue:
prefix = "👷[WARN]👷"
case LogLevel.error.rawValue:
prefix = "🪦[ERROR]🪦"
default:
prefix = ""
}
return "\(prefix)\(message)"
}
}
実際に使ってみる、以下。
import SwiftUI
struct HomeView: View {
var body: some View {
VStack {
Label("Hello, World!", systemImage: "apple.logo")
}
.onAppear {
Logger.debug("Hello Debug")
Logger.info("Hello Info")
Logger.event("Hello Event")
Logger.error("Hello Error")
}
}
}