Simple Log Facade for Swift serves as a simple facade for logging frameworks allowing the end user to plug in the desired logging framework at deployment time
let myLogger = SLF4Swift.getLogger("loggerName")
myLogger.info("my info message")
myLogger.log(.Error, "my error message")
Not already chosen a logging framework? Don't want to be tied to a specific framework and want to switch (or even combine) easily? Why not uing a logging facade?
A Simple Logging Facade
print()
or NSLog()
or commented debugging print codeGetting the default one
let myLogger = SLF4Swift.defaultLogger
Get logger using a key or create if not exist
let myLogger = SLF4Swift.createLogger("loggerKey")
Or just get it - here logger could be nil if not already created
if let myLogger = SLF4Swift.getLogger("loggerKey") {..}
A key could be a framework name, a class name, a topic name, ....
If you use a key into a framework, create a public
constant to allow the end user to decide according to this key what to do with your logs (see Custom factories and loggers)
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())
}
There is also macro functions for default logger
SLFLogInfo("info message")
SLFLog(.Verbose, "verbose message")
...
By declaring typealias Log = SLFLogLevel
you can also use default logger like this
Log.Info.message("info message")
Log.Verbose.message("verbose message")
Log.Verbose.trace()// print file, function and line
By default is installed a NullLoggerFactory
, which disable all logs
SLF4Swift.setSharedFactory(NullLoggerFactory.instance)
To enable log, set your LogFactoryType
SLF4Swift.setSharedFactory(MyCustomLoggerFactory())
or add DEBUG
to "Other Swift Flags" into "Swift Compiler - Custom Flags" section
this add by default a SLFLoggerFactory
, which use by default print()
SLF4Swift.setSharedFactory(SLFLoggerFactory.sharedInstance)
Some backend factories are already implemented into backend folder (see setup for pod)
SLF4Swift.setSharedFactory(CocoaLumberjackMacroLoggerFactory.instance)
You can your create your own LogFactoryType
and/or 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)
}
To create a custom LogFactoryType
extend
SingleLoggerFactory
if your factory use only one loggerProxyLoggerFactory
if your want to use an another factory and make some adaptationsSLFLoggerFactory
and override doCreateLogger
, if you want to store logger into a Dictionnary
by keySome basic loggers are already implemented into implementation folder (log to a file, using nslog, ...)
Don't hesitate to fork this repository and PR additionnal LogFactoryType
for your logging framework
See CONTRIBUTING.md
Add pod 'SLF4Swift'
to your Podfile
and run pod install
.
Add use_frameworks!
to the end of the Podfile
.
CocoaLumberjack
pod 'SLF4Swift/CocoaLumberjack'
XCGLogger
pod 'SLF4Swift/XCGLogger'
SpeedLog
pod 'SLF4Swift/SpeedLog'
Loggerithm
pod 'SLF4Swift/Loggerithm'
In podspec
s.dependency 'SLF4Swift'
Add pod 'SLF4Swift/Impl'
to your Podfile
__FILE__
, __LINE__
, __FUNCTION__
). If there is a way, I am interested to learn itThe MIT License (MIT), see LICENSE file