別の解決策は、このディスカッションで利用できます。それは今のところ私にとってはうまくいき、私はそれを試しています.
UnitDDLoggable
定義して採用する必要があります
var logPrefix = ""
var logLevel = DDLogLevel.Debug
簡単に書くことができます:
DDLogWarn("Danger, Will Robinson"
Swift 2.3 のコード:
import CocoaLumberjackSwift
/// Base protocol for unit specific logging. Generally you won't implement this protocol directly, you will
/// implement one of the protocols that inherit from it
protocol UnitLoggable {
/// Prefix to append to each log line, should include a trailing space to separate it from the log message
var logPrefix:String { get }
}
/// Implment this protocol to use CocoaLumberjack logging with the level controlable at the file level
protocol UnitDDLoggable : UnitLoggable {
/// Lumberjack log level to use for this code unit, Lumberjack log calls in this unit will use this level
/// not the default log level, to use the shared lumberjack level this property should return defaultDebugLevel
var logLevel:DDLogLevel { get }
}
extension UnitDDLoggable {
final func DDLogDebug(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Debug, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogInfo(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Info, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogWarn(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Warning, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogVerbose(@autoclosure logText: () -> String, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true) {
SwiftLogMacro(async, level: logLevel, flag: .Verbose, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
final func DDLogError(@autoclosure logText: () -> String,context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = false) {
SwiftLogMacro(async, level: logLevel, flag: .Error, context: context, file: file, function: function, line: line, tag: tag, string: logPrefix + logText())
}
}