KYLogger

一个适用于 Apple 平台的本地系统日志记录器。

这个轻量级日志记录库是无需服务器的。它包含一个调试日志记录器和一个文件日志记录器,它们会将日志本地保存在设备上。用户可以选择在报告错误时发送日志文件。


macOS iOS watchOS
SPM CocoaPods Carthage

安装

请参阅以下小节,了解有关不同安装方法的详细信息。

KYLog 用法

KYLog(.debug, "A debug message")
KYLog(.notice, "A notice message")
KYLog(.success, "A success message")
KYLog(.warn, "A warn message")
KYLog(.error, "A error message")
KYLog(.critical, "A critical message")

输出

🟣 DEBUG -[KYLoggerDemoApp.swift init()] L18: A debug message
🔵 NOTICE -[KYLoggerDemoApp.swift init()] L19: A notice message
🟢 SUCCESS -[KYLoggerDemoApp.swift init()] L20: A success message
🟡 WARN -[KYLoggerDemoApp.swift init()] L21: A warn message
🔴 ERROR -[KYLoggerDemoApp.swift init()] L22: A error message
❌ CRITICAL -[KYLoggerDemoApp.swift init()] L23: A critical message

KYFileLogger 用法

你可以定义一个全局变量来开关文件日志记录,并使用便捷函数封装日志记录器。就像示例项目一样

class DemoAppFileLogger {
  public static var isDataSyncLoggingEnabled: Bool = false
}

public func DemoAppSyncFileLog(
  _ type: KYLogType,
  _ message: String,
  function: String = #function,
  file: String = #file,
  line: Int = #line
) {
#if DEBUG
  KYFileLogger.log(type, message, DemoAppFileLogger.isDataSyncLoggingEnabled, function: function, file: file, line: line)
#else
  if DemoAppFileLogger.isDataSyncLoggingEnabled {
    KYFileLogger.log(type, message, function: function, file: file, line: line)
  }
#endif
}

// This message will be saved to disk only if `DemoAppFileLogger.isDataSyncLoggingEnabled = true`.
DemoAppSyncFileLog(.notice, "Some sync messages.")

注意

你可以查看KYLoggerDemo 示例项目以了解更多详情。