SLF4Swift 作为简单日志包装器,允许用户在部署时将所需的日志框架插入到框架中
let myLogger = SLF4Swift.getLogger("loggerName")
myLogger.info("my info message")
myLogger.log(.Error, "my error message")
一个简单的日志封装器
println()
或 NSLog()
,并让最终用户捕获这些日志获取默认的一个
let myLogger = SLF4Swift.defaultLogger
获取 logger 或如果不存在则创建它
let myLogger = SLF4Swift.createLogger("loggerKey")
或者获取它,在这里 logger 可能为 nil
if let myLogger = SLF4Swift.getLogger("loggerKey") {..}
在框架中使用公钥获取 logger,然后最终用户可以创建一个 LogFactoryType
并根据此密钥决定返回哪个 logger
myLogger.log(.Error, "my error message") // basic method
// and some shortcut
myLogger.info("my info message")
myLogger.warn("my warn message")
...
// if message compute in a long period of time
if myLogger.isLoggable(.Verbose) {
myLogger.log(.Verbose, createLongMessageClosure())
}
if myLogger.exec(.Verbose) { // or with closure
myLogger.log(.Verbose, createLongMessageClosure())
}
有默认 logger 的宏函数
SLFLogInfo("info message")
SLFLog(.Verbose, "verbose message")
...
通过声明 typealias Log = SLFLogLevel
,您也可以这样使用默认 logger
Log.Info.message("info message")
Log.Verbose.message("verbose message")
Log.Verbose.trace()// print file, function and line
默认情况下,安装了一个 NullLoggerFactory
,该 factory 禁用了日志记录
SLF4Swift.setSharedFactory(NullLoggerFactory.instance)
为了启用日志记录,设置您的 LogFactoryType
SLF4Swift.setSharedFactory(MyCustomLoggerFactory())
或向 “Swift 编译器 - 自定义标志” 部分 “其他 Swift 标志” 中添加 DEBUG
,这将默认添加一个 SLFLoggerFactory
,该 factory 默认使用 println()
SLF4Swift.setSharedFactory(SLFLoggerFactory.sharedInstance)
您可以创建自己的 LogFactoryType
和/或 LoggerType
class MyCustomLoggerFactory: ProxyLoggerFactory {
func getLogger(name: LoggerKeyType) -> LoggerType? {
if name == AFrameWorkKey {
return NullLogger.instance // deactive log for specifc logger
}
else if name == AnOtherFrameWorkKey {
var logger = super.getLogger(name)
logger.level = .DEBUG // set a specific log level
return logger
}
return super.getLogger(name)
}
扩展
SingleLoggerFactory
如果您的工厂只使用一个 loggerProxyLoggerFactory
如果您想使用另一个工厂并进行一些调整SLFLoggerFactory
并覆盖 doCreateLogger
,如果您想将 logger 存储到 Dictionnary
中并按键一些基本的 logger 已经在 实现文件夹 中实现
一些后端工厂已经集成到后端文件夹中(见配置)
SLF4Swift.setSharedFactory(CocoaLumberjackMacroLoggerFactory.instance)
请毫不犹豫地将该仓库Fork并提交增加LogFactoryType
的PR,以用于您自己的日志框架
CocoaLumberjack pod 'SLF4Swift/CocoaLumberjack'
XCGLogger pod 'SLF4Swift/XCGLogger'
SpeedLog pod 'SLF4Swift/SpeedLog'
在podspec中
s.dependency 'SLF4Swift'
将pod 'SLF4Swift/Impl'
添加到您的Podfile
中
LogFactoryType
的实现String
替换为Printable
对象或@autoclosure () -> Printable
?Hashable
?LoggerType
?__FILE__
、__LINE__
、__FUNCTION__
)。如果有一种方法,我对此很感兴趣The MIT License (MIT)
Copyright (c) 2015 Eric Marchand (phimage)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
由kodlian设计灵感来自apple swift logo
我喜欢使用SourceTree浏览项目时看到每个项目的图像