Integrating LogglyLogger-CocoaLumberjack in swift project

276 views Asked by At

I am trying to use LogglyLogger-CocoaLumberjack in my swift project.

I am getting this error in xCode.

Enum case 'verbose' has no associated values

I am unable to resolve this.

https://prnt.sc/uznr01

I am actually trying to translate the Objective-C code in swift 5. Here is my swift function

in appDelegate.swift class

func initLoggly(){
        

        //      static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
                let ddLogLevel:DDLogLevel = .verbose
                
        //      LogglyLogger *logglyLogger = [[LogglyLogger alloc] init];
                let logglyLogger = LogglyLogger()
        
        //      [logglyLogger setLogFormatter:[[LogglyFormatter alloc] init]];
                logglyLogger.logFormatter = LogglyFormatter()
        
        //      logglyLogger.logglyKey = @"your-loggly-api-key";
                logglyLogger.logglyKey = "XXXXXXXXXXXX-XXXXXX"
                
        //
        //        // Set posting interval every 15 seconds, just for testing this out, but the default value of 600 seconds is better in apps
        //        // that normally don't access the network very often. When the user suspends the app, the logs will always be posted.
        //      logglyLogger.saveInterval = 15;
                logglyLogger.saveInterval = 15
        
        //      [DDLog addLogger:logglyLogger];
                DDLog.add(logglyLogger)
                
        //        // Do some logging
        //      DDLogVerbose(@"{\"myJsonKey\":\"some verbose json value\"}");
        //     ddLogLevel.verbose("{\"initloggly\":\"some verbose json value\"}") // also tried this, error ==> Enum case 'verbose' cannot be used as an instance member
                DDLogLevel.verbose("{\"initloggly\":\"some verbose json value\"}") // Here is the error on this line

    }

Please point out what I am doing wrong!

1

There are 1 answers

3
Gereon On

This library is heavly based on C preprocessor macros which aren't accessible from Swift.

You will probably need to write a small set of wrapper functions in Objective-C that use these macros and are in turn callable from Swift.

Here's an example of how this could look like:

LogglyWrapper.h:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
@interface LogglyWrapper : NSObject
+(void) logVerbose:(NSString*) msg;
@end
NS_ASSUME_NONNULL_END

and LogglyWrapper.m:

#import "LogglyWrapper.h"
#import <LogglyLogger.h>

@implementation LogglyWrapper
static const DDLogLevel ddLogLevel = DDLogLevelVerbose;

+(void) logVerbose:(NSString*) msg {
    DDLogVerbose(@"%@", msg);
}
@end

usage from Swift:

LogglyWrapper.logVerbose("foo")