SLLog 是一个简单而优雅的 swift 日志记录器。允许您将内容记录到文件、控制台或您的自定义目标。
1990-02-19T22:45:36.250Z [VERBOSE] MyFile.swift:19 - 123
1991-03-20T20:33:44.777Z [INFO] MyFile.swift:20 - ABC
1992-04-21T09:53:51.021Z [DEBUG] MyFile.swift:21 - @$#!^%
1993-05-22T11:05:02.000Z [WARNING] MyFile.swift:22 - 2017-10-04 22:45:36 +0000
1994-06-23T15:13:00.146Z [ERROR] MyFile.swift:23 - [0.10000000000000001, 1, "A", 2017-10-04 09:55:36 +0000]
将以下依赖项添加到您的 Package.swift
文件中
.package(url: "https://github.com/SLLog/SLLog", from: "1.1.0"),
在您的文件顶部导入
import SLLog
使用 Log
类记录您需要的任何信息
Log.d("ABC")
Log.w("#%^$&@")
Log.e("1233")
任何对象。
SLLog.addHandler(SLLogConsole())
Log.v(123)
Log.i("ABC")
Log.d("@$#!^%")
Log.w(Date())
Log.e([0.1,1,"A",Date()])
设置 SLLogger
SLLog.addHandler(try! SLLogFile("path/to/directory"))
或控制台处理程序
SLLog.addHandler(SLLogConsole())
或两者都
SLLog.addHandler(SLLogConsole(), try! SLLogFile(path))
您可以创建自定义日志处理程序。只需遵循 LogHandler
协议。
public class MyHandler: LogHandler {
open func handle(message: Any, level: SLLog.LogType, file: String, line: UInt) {
//Do your stuff with log.
}
}
然后将其添加到 SLLog
SLLog.addHandler(MyHandler())
SLLog 可以有自定义提供程序。要添加您自己的提供程序
struct MySLLogProvider: LogProvider {}
SLLog.addProvider(MySLLogProvider())
您可以使用 LogProvider
内置的 send 方法。通过 SLLog 发送您的日志。
默认控制台日志
isDebug: Bool = true,
isTerminal: Bool = {
#if Xcode
return false
#else
return true
#endif
}(),
minProductionLogType: UInt = 3
logFormat: String = ":d :t :f::l :m",
dateFormat: String? = nil,
logColors: [SLLog.LogType:LogColor]? = nil
重写在初始化器中传递的参数以更改 ConsoleLog,例如 SLLogConsole(isDebug: true, isTerminal: false)
或 SLLogConsole(isTerminal: false)
等等。使用 isTerminal 属性来选择终端或控制台设置。SLLogConsole(isTerminal: false)
要完全配置 SLLog,请在初始化期间传递 Configuration
对象。public init(_ config: Configuration = Configuration())
默认情况下,日志设置为终端。日志格式为 ":d :t :f::l :m"
,其中
`:d` is replaced in string to display date
`:t` is replaced by log type
`:f` is replaced by file name
`:l` is replaced by line number
`:m` is replaced by message object
记录器使用的默认日期格式如下:"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
要去除默认日志颜色,请在 logColors
中传递空字典或包含您自己定义的字典。有关终端颜色的更多信息,请查看:https://misc.flogisoft.com/bash/tip_colors_and_formatting
LogColor 定义如下:init(_ terminal: String, _ console: String)
。对于 XCode 控制台,表情符号用作颜色。
SLLog.LogType.verbose:LogColor(TerminalColor.lightGray, "☑️"),
SLLog.LogType.info:LogColor(TerminalColor.lightCyan, "Ⓜ️"),
SLLog.LogType.debug:LogColor(TerminalColor.lightGreen, "✅"),
SLLog.LogType.warning:LogColor(TerminalColor.lightYellow, "⚠️"),
SLLog.LogType.error:LogColor(TerminalColor.lightRed, "⛔️"),
使用 TerminalColor
快速访问预定义的颜色值
SLLogFile 将日志保存到每天创建的文件中。文件名如下:"yyyy-MM-dd"
。创建在 :yourPath/sllogs
下,扩展名为 .log
。默认 maxFilesCount
设置为 3
,这意味着记录器一次存储 3 个文件。1 个文件 = 1 天的日志
欢迎为这个项目做贡献!:)
您可以在 GitHub 上创建一个 issue。
本项目已在 MIT 许可证下发布。