1

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
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119

3 Answers3

3

For those still looking for an easy way to change logging levels dynamically in Swift, simply use dynamicLogLevel at any time. For example:

dynamicLogLevel = .info
Gary Hooper
  • 312
  • 1
  • 13
1

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!

acj
  • 4,821
  • 4
  • 34
  • 49
1

Another solution is available in this discussion . It works for me for the moment and I am experimenting with it.

You have to adopt UnitDDLoggable by defining

  var logPrefix = ""
  var logLevel = DDLogLevel.Debug

It allows one to simply write :

DDLogWarn("Danger, Will Robinson"

The code for 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())
    }
}
Adrian
  • 1,595
  • 1
  • 19
  • 21