Search code examples
iosswiftcocoalumberjack

CocoaLumberjack Dynamic Logging in Swift


I am trying to use CocoaLumberjack in Swift. Using pod 'CocoaLumberjack/Swift'

In objective C I do the following

int ddLogLevel = DDLogLevelOff;

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

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

In Swift I do not understand how to do this

I made a class which implemented DDRegisteredDynamicLogging

This gives me two methods

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

However I am still unclear where and how to declare DDLogLevel to set and get

The equivalent of int ddLogLevel = DDLogLevelOff;

I tried

static var ddLogLevel: DDLogLevel = defaultDebugLevel

Solution

  • It's not ideal, but I've gotten this to work for CocoaLumberjack 2.2.0 as follows:

    • Add the following to your class:

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

      This ensures that CocoaLumberjack will identify your class as registered for logging purposes and will enable you to change its logging level at run time.

    • When logging, use e.g.

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

      The level parameter is crucial. Without it, the message will always be logged.

    My hope is that CocoaLumberjack's Swift support will mature and remove these stumbling blocks. Until then, happy logging!