用法
使用预先捆绑的日志类Logger
,您可以使用日志级别发送日志
// Global context
let logger = Logger()
let stdout = StandardOut()
logger.register(destination: stdout)
// In some code
logger.debug("For debug")
logger.verbose("Something verbose")
logger.info("Something want to know")
logger.warning("Not expected, but not error")
logger.error("Something went wrong, fix this")
定制
您可以注册自定义目的地和日志格式。日志格式属于目的地。因此,您可以根据每个目的地设置格式。
// Create your log formatter
final class CustomLogFormatter: LogFormatterProtocol {
func format(message: Any, level: LogLevel, context: LogContextProtocol) -> String {
// Format message here
}
}
// Create you log destination
final class CustomLogDestination: LogDestinationProtocol {
var formatter: LogFormatterProtocol
init(formatter: LogFormatterProtocol = CustomLogFormatter()) {
self.formatter = formatter
}
func write(_ message: Any, level: LogLevel, context: LogContextProtocol) {
let formatted = formatter.format(message, level: level, context: context)
// You can write here how you want
}
}
安装
使用 CocoaPods
pod 'LoggerKit'
开发
为了创建 Xcode 项目,运行
$ swift package generate-xcodeproj
发布
$ edit LoggerKit.podspec
# set the new version to 0.0.1
# set the new tag to 0.0.1
$ pod lib lint
$ git add -A && git commit -m "Release 0.0.1."
$ git tag '0.0.1'
$ git push --tags
$ pod trunk push LoggerKit.podspec