CocoaLumberjack在Swift中动态记录

时间:2015-12-02 16:49:23

标签: ios swift cocoalumberjack

我正在尝试在Swift中使用CocoaLumberjack。 使用pod' CocoaLumberjack / Swift'

在目标C中,我执行以下操作

int ddLogLevel = DDLogLevelOff;

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

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

在Swift中,我不明白该怎么做

我创建了一个实现DDRegisteredDynamicLogging

的类

这给了我两个方法

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

但是我仍然不清楚在哪里以及如何声明DDLogLevel来设置和获取

相当于 int ddLogLevel = DDLogLevelOff ;

我试过

static var ddLogLevel: DDLogLevel = defaultDebugLevel

3 个答案:

答案 0 :(得分:2)

对于仍在寻找在Swift中动态更改日志记录级别的简便方法的人,只需随时使用dynamicLogLevel。例如:

dynamicLogLevel = .info

答案 1 :(得分: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支持能够成熟并消除这些绊脚石。在那之前,快乐的伐木!

答案 2 :(得分:1)

discussion中提供了另一种解决方案。它暂时适用于我,我正在尝试它。

您必须通过定义

来采用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())
    }
}