LoginSignup
4
2

More than 1 year has passed since last update.

【Swift】Xcodeのログを見やすくしたい

Last updated at Posted at 2022-12-24

はじめに

成功or失敗で条件分岐させた時にどこが実行されたか一目でわかるようにしたいです。
そう思った時にログに色を付けられたらいいのでは?と思ったのですが出来なさそうなので別の方法を考えました。

🟥🟧🟨🟩🟦🟪🟫⬛⬜

このような絵文字を使用してログを出力できるようにします。

実装

public enum LogType {
    case verbose
    case info
    case debug
    case success
    case warning
    case error
    case custom(String)
}


public func print(_ type: LogType, _ message: String) {
    switch type {
    case .verbose:
        print("⬜-VERBOSE: \(message)")
    case .info:
        print("🟦-INFO: \(message)")
    case .debug:
        print("🟧-DEBUG: \(message)")
    case .success:
        print("🟩-SUCCESS: \(message)")
    case .warning:
        print("🟨-WARNING: \(message)")
    case .error:
        print("🟥-ERROR: \(message)")
    case let .custom(emoji):
        print("\(emoji): \(message)")
    }
}

使い方

// verbose
print(.verbose, "This is an example of an verbose log.")
// info
print(.info, "This is an example of an info log.")
// debug
print(.debug, "This is an example of an debug log.")
// success
print(.success, "This is an example of an success log.")
// warning
print(.warning, "This is an example of an warning log.")
// error
print(.error, "This is an example of an error log.")
// custom
print(.custom("🚀-ROCKET"), "This is an example of an custom log.")

こんな感じで表示されます。
まぁまぁ見やすいですね
log

おわり

本当はログに色を付けたい。。。
できないのだろうか。。。

ライブラリにしてみました
スターください

4
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2