Global log level for CocoaLumberjack

后端 未结 12 996
[愿得一人]
[愿得一人] 2020-12-23 20:19

I\'m using CocoaLumberjack in an iPhone project, to log some information.

I\'ve followed the Getting started guide, and everything works fine, but there is one thing

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 21:01

    In order to dynamically inject log level (for example, from configuration file):

    1) Create a new class named DDLogLevel with the following code:

    #import "DDLogLevel.h"
    #import "DDLog.h"
    
    @implementation DDLogLevel
    
    static int _ddLogLevel = LOG_LEVEL_VERBOSE;
    
    + (int)ddLogLevel
    {
        return _ddLogLevel;
    }
    
    + (void)ddSetLogLevel:(int)logLevel
    {
        _ddLogLevel = logLevel;
    }
    
    @end
    

    2) In DDLogLevel.h, find the row that contains the following statement:

    #ifndef LOG_LEVEL_DEF
        #define LOG_LEVEL_DEF ddLogLevel
    #endif
    

    And replace it with:

    #ifndef LOG_LEVEL_DEF
        #define LOG_LEVEL_DEF [DDLogLevel ddLogLevel]
    #endif
    

    3) Finally, call from your Initialization process (perhaps from appDelegate) to ddSetLogLevel with the desired level.

提交回复
热议问题