CocoaLumberjack with Swift - Calling preprocessor macros

后端 未结 3 1702
鱼传尺愫
鱼传尺愫 2020-12-28 10:32

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 CustomLoggerFor

相关标签:
3条回答
  • 2020-12-28 11:16

    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")
    
    0 讨论(0)
  • 2020-12-28 11:21

    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");
    
    0 讨论(0)
  • 2020-12-28 11:23

    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")
    
    0 讨论(0)
提交回复
热议问题