使用 XCGLogger 紀錄日誌

Bob Chang
Bob Chang
Nov 5 · 2 min read

使用 Swift 開發,我們習慣使用 print(_ items: Any)

func print(_ items: Any…, separator: String = “ “, terminator: String = “\n”)

但是 XCGLogger 順帶紀錄了上述打印方式沒有的除錯資訊。

  • 打印日誌時戳
  • 打印日誌等級
  • 打印日誌當前 block 所使用的 Thread 名稱
  • 使用的類別檔案名稱
  • 使用的類別檔案中的行數
  • 打印訊息的方法名
  • 打印訊息

以下是使用 XCGLogger 打印出來的 log 範例。

2014–06–09 06:44:43.600 [Debug] [main] [AppDelegate.swift:40] application(_:didFinishLaunchingWithOptions:): Simple message

此工具可以讓你的 log 有幾種程度分級:

  • severe (最嚴重)
  • error
  • warning
  • info
  • debug
  • verbose (最輕微)

你可以設定在 console 裡只顯示某個等級以上的 log,其他的 filter 掉。

他也提供將這些 log 儲存成檔案,該方式亦可設定 fliter 的 log 等級。

更多使用方式細節請參閱 原作 github ReadMe Basic Usage

參考 Google Blogger 本文

https://bob910078.blogspot.com/2019/11/xcglogger-framework.html

喜歡橘子 / 認為活得快樂才是生命真理 / 互相學習求進步 / 我的部落格 https://bob910078.blogspot.com / 或是追蹤 Twitter @bob910078

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade