JJSwiftLog 0.1.3

JJSwiftLog 0.1.3

jezz维护。



  • 作者:Jezz
  • Jezz

Swift4.0+ CocoaPods Compatible Carthage Compatible Platform License MIT

JJSwiftLog

JJSwiftLog screenshot

保持Swift日志简洁,同时考虑基本的日志功能,如文件名、函数名、行数等信息,内置控制台和文件日志功能,满足开发者基本需求,为开发者提供高度灵活的自定义日志。

特点

  • 控制台显示(控制台日志),考虑了NSLog的功能

  • 日志文件存储(文件日志),配置文件日志的扩展属性

  • 用户定义的日志,继承自JJLogObject

  • 全局日志开关

  • 仅显示指定的文件日志

  • 自定义日志过滤器

  • 自定义日志格式,任意组合,提供内置样式供开发者选择,内置ANSIColor格式

  • 支持多平台:iOS、MacOS、Windows和Linux

安装

  • Swift 4.0+

Podfile

pod 'JJSwiftLog'

Carthage

github "jezzmemo/JJSwiftLog"

Swift包管理器

.package(url: "https://github.com/jezzmemo/JJSwiftLog.git"),

如何使用

快速使用

  • 导入模块
import JJSwiftLog
  • 快速开始

通常推荐在程序入口处初始化。控制台和文件日志默认配置。你只需要配置日志级别。示例

override func viewDidLoad() {
    super.viewDidLoad()
    JJLogger.setup(level: .verbose)

    JJLogger.verbose("verbose")
    JJLogger.debug("debug")
    JJLogger.info("info")
    JJLogger.warning("warn")
    JJLogger.error("error")
}

高级

  • 开发者配置日志
override func viewDidLoad() {
     super.viewDidLoad()
     // filePath needs to store the path
     var file = JJFileOutput(filePath: "filePath", delegate: JJLogger, identifier: "file")
     file?.targetMaxFileSize = 1000 * 1024
     file?.targetMaxTimeInterval = 600
     file?.targetMaxLogFiles = 20
     JJLogger.addLogOutput(file)
     #if DEBUG
     JJLogger.addLogOutput(JJConsoleOutput(identifier: "console"))
     #endif
     // Note the timing of the startLogInfo call
     JJLogger.startLogInfo()
     JJLogger.verbose("verbose")
     JJLogger.debug("debug")   
     JJLogger.info("info")
     JJLogger.warning("warn")
     JJLogger.error("error")
     // Any type
     JJLogger.verbose(123)
     JJLogger.debug(1.2)
     JJLogger.info(Date())
     JJLogger.warning(["1", "2"])
}
  • JJConsoleOutput 使用 NSLog 样式,使用 isUseNSLog 属性
let console = JJConsoleOutput()
console.isUseNSLog = false
  • JJFileOutput 有几个属性可以调整存储文件策略

    • targetMaxFileSize 最大文件大小

    • targetMaxTimeInterval 生成新文件的间隔

    • targetMaxFileSize 文件的最大数量,如果超过此数量,则删除以前的文件

let file = JJFileOutput()
file?.targetMaxFileSize = 1000 * 1024
file?.targetMaxTimeInterval = 600
file?.targetMaxLogFiles = 20
  • enable 设置为开启实时日志记录,默认开启
JJLogger.enable = true
  • onlyLogFile 方法设置为只显示指定文件的日志
JJLogger.onlyLogFile("ViewController")
  • JJSwiftLog 支持自定义格式化日志。以下表格是缩写字母的对应关系
缩写 描述
%M 日志文本
%L 日志级别
%l 日志行号
%F 文件名,不带后缀
%f 函数名
%D 日期(目前只支持 yyyy-MM-dd HH:mm:ss.SSSZ 格式)
%T 线程,如果为主线程则显示为 main,为子线程则显示地址或 QueueLabel
%t 显示 HH:mm:ss 格式
%d 显示 yyyy-MM-dd 格式
%N 文件名,带后缀

示例

JJLogger.format = "%M %F %L%l %f %D"

还有内置样式,如:JJLogger.format = JJSwiftLog.simpleFormat,示例

2020-04-08 22:56:54.888+0800 -> ViewController:18 - setupVendor(parameter:) method set the parameter
2020-04-08 22:56:54.889+0800 -> ViewController:28 - viewDidLoad() Start the record
2020-04-08 22:56:54.889+0800 -> ViewController:29 - viewDidLoad() Debug the world
2020-04-08 22:56:54.890+0800 -> ViewController:30 - viewDidLoad() Show log info
2020-04-08 22:56:54.890+0800 -> ViewController:31 - viewDidLoad() Build warning
2020-04-08 22:56:54.890+0800 -> ViewController:32 - viewDidLoad() can’t fetch user info without user id
  • 根据需要实现自定义接口 JJLogObject,示例
public class CustomerOutput: JJLogObject {
    ///重写output即可
    open override func output(log: JJLogEntity, message: String) {

    }
    
}
  • 每个 JJLogObject 都对应一个 formatters(格式化)和 filters(过滤)属性。你可以根据自己的需求自定义格式化和过滤。示例
open class CustomerFormatter: JJLogFormatterProtocol {
    public func format(log: JJLogEntity, message: String) -> String {
        return ""
    }
}
open class CustomerFilter: JJLogFilter {
    func ignore(log: JJLogEntity, message: String) -> Bool {
        return false
    }
}
  • 内置 JJFormatterLogANSIColor,可以使用终端带颜色查看日志,只需在 formatters 中添加以下内容

xcode 控制台不支持 ANSIColor 模式,目前仅在终端上测试

let file = JJFileOutput(delegate: JJLogger, identifier: "file")
file?.targetMaxFileSize = 1000 * 1024
file?.targetMaxTimeInterval = 600
file?.targetMaxLogFiles = 20
file?.formatters = [JJFormatterLogANSIColor()]
JJLogger.addLogOutput(file!)

示例

JJSwiftLog ANSIColor

  • 支持 Sentry,示例:
let sentry = JJSentryOutput(sentryKey: "key", 
sentryURL: URL(string: "http://www.exmaple.com/api/5/store/")!, delegate: JJLogger)
sentry.completion = { result in
}
sentry.failure = { error in
}
JJLogger.addLogOutput(sentry)

链接器

许可证

JJSwiftLog 在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。