8

I started to build an IOS app with the new programming language Swift. I managed to use CocoaPods and was able to successfully create the DDTTYLogger with my CustomLoggerFormatter (Objective-C) in my AppDelegate.swift and append it to the loggers.

var customLoggerFormatter = CustomLoggerFormatter()

var consoleLogger: DDTTYLogger = DDTTYLogger.sharedInstance()
consoleLogger.setLogFormatter(customLoggerFormatter)
DDLog.addLogger(consoleLogger)

But the problem is, that the CocoaLumberjack Library is using preprocessor macros for the logger methods like DDLogVerbose(@"..")

Which is defined in the DDLog.h:

#define DDLogVerbose(frmt, ...) LOG_OBJC_MAYBE(LOG_ASYNC_VERBOSE, LOG_LEVEL_DEF, LOG_FLAG_VERBOSE, 0, frmt, ##__VA_ARGS__)

Is there any workaround to make preprocessor defines work in Swift? Or did anyone try something similar with more success?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Prine
  • 12,192
  • 8
  • 40
  • 59

3 Answers3

12

Okay, I just found a solution. Writing an Objective-C Wrapper class calling the preprocessors and offering methods to call it.

Hopefully this will help other people facing the same issues.

I first created a header file:

@interface DDLogWrapper : NSObject
+ (void) logVerbose:(NSString *)message;
+ (void) logError:(NSString *)message;
+ (void) logInfo:(NSString *)message;
@end

With the corresponding implementation:

#import <Foundation/Foundation.h>
#import "DDLogWrapper.h"

// Logging Framework Lumberjack
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"

// Definition of the current log level
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_ERROR;
#endif

@implementation DDLogWrapper

+ (void) logVerbose:(NSString *)message {
    DDLogVerbose(message);
}

+ (void) logError:(NSString *)message {
    DDLogError(message);
}

+ (void) logInfo:(NSString *)message {
    DDLogInfo(message);
}

@end

Important is to add the DDLogWrapper.h File to the ProjectName-Bridging-Header.h file and then you are able to instantiate in Swift the DDLogWrapper and call the methods logVerbose, logError, logInfo..

With the following code I was able to make a log statement:

DDLogWrapper.logVerbose("TEST");
Prine
  • 12,192
  • 8
  • 40
  • 59
  • How do you define the `ProjectName-Bridging-Header.h`? I tried `#import ` and Xcode is complaining `file not found` error. `DDLogWrapper.h` is in directory `ProjectName/Application/DDLogWrapper.h`. I also tried `Application/DDLogWrapper.h` and that didn't work. My `SWIFT_OBJC_BRIDGING_HEADER` field in `Build Settings` is blank but I have no problems bridging other modules. – netwire Aug 19 '14 at 22:43
  • Per an attempted edit by [runios](http://stackoverflow.com/users/3944596/runios), you also need `#import "DDLogMacros.h"`. – admdrew Sep 29 '14 at 19:03
  • [CocoaLumberjack supports Swift & Objective-C](https://github.com/CocoaLumberjack/CocoaLumberjack#swift-version-via-cocoapods) if you're migrating your app. `pod 'CocoaLumberjack/Swift'` Requires iOS8 minimum. If you're stuck supporting iOS7 like me then this works a treat. – mriddle89 Aug 17 '16 at 23:41
6

As of 2.0.0beta4, CocoaLumberJack includes a CocoaLumberJack.swift file that makes its integration with Swift projects really easily.

They use a global var defaultDebugLevel to set the DDLogLevel, and you can swift basic precompile macros to customize it to your needs.

#if DEBUG
    defaultDebugLevel = DDLogLevel.All
#else
    defaultDebugLevel = DDLogLevel.Warning
#endif

DDLog.addLogger(DDTTYLogger.sharedInstance())
DDLogDebug("Debug")
DDLogInfo("Info")
DDLogWarn("Warning")
DDLogVerbose("Verbose")
DDLogError("Error")
apouche
  • 9,703
  • 6
  • 40
  • 45
  • 1
    I use cocoapods 0.36.0.beta.2 and lumberjack 2.0.0-rc. I have the swift file in Pods ▸ CocoaLumberjack ▸ Classes ▸ CocoaLumberjack.swift. In the swift file in my project, I try to import it but I get `No such module 'CocoaLumberjack'`, ideas? – lawicko Feb 19 '15 at 10:51
  • lumberjack 2.1.0 now has swift 2.0 support supposedly. – John Shelley Oct 12 '15 at 22:38
5

I created a Swift wrapper for CocoaLumberjack that encapsulates everything nicely.

DDLog.addLogger(DDTTYLogger.sharedInstance())
DDLog.logLevel = .Info

logInfo("Info")
logWarn("Warn")
logDebug("Debug")
logError("Error")
stigi
  • 6,661
  • 3
  • 40
  • 50