2015-12-02 41 views
1

我想在Swift中使用CocoaLumberjack。 使用莢'CocoaLumberjack /雨燕CocoaLumberjack Swift動態日誌記錄

在客觀CI做以下

int ddLogLevel = DDLogLevelOff; 

@implementation CLDDLoglevel 
+ (int)ddLogLevel 
{ 
    return ddLogLevel; 
} 

+ (void)setLogLevel:(int)logLevel 
{ 
    ddLogLevel = logLevel; 
} 

在斯威夫特,我不知道如何做到這一點

我做這實現DDRegisteredDynamicLogging

類這給我兩種方法

static func ddLogLevel() -> DDLogLevel { 
} 
static func ddSetLogLevel(level: DDLogLevel) { 
} 

但是我仍然不清楚在哪裏以及如何聲明DDLogLevel來設置和獲取

等效於int ddLogLevel = DDLogLevelOff;

我試圖

static var ddLogLevel: DDLogLevel = defaultDebugLevel 

回答

1

這並不理想,但我已經得到這個爲CocoaLumberjack 2.2.0工作方式如下:

  • 以下內容添加到您的類:

    static var ddLogLevel: DDLogLevel = .Off 
    
    static func ddSetLogLevel(level: DDLogLevel) { 
        ddLogLevel = level 
    } 
    

    這可確保CocoaLumberjack將您的班級標識爲註冊用於登錄目的,並將啓用您在運行時更改其日誌記錄級別。

  • 當記錄時,使用例如

    DDLogWarn("Danger, Will Robinson", level: self.dynamicType.ddLogLevel) 
    

    level參數是至關重要的。沒有它,該消息將始終被記錄。

我的希望是CocoaLumberjack的Swift支持將會成熟並消除這些絆腳石。在此之前,快樂的伐木!

1

discussion中提供了另一種解決方案。它目前適用於我,我正在試驗它。

你必須定義

var logPrefix = "" 
    var logLevel = DDLogLevel.Debug 

採用UnitDDLoggable它允許一個簡單的寫:

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()) 
    } 
}