Global log level for CocoaLumberjack

后端 未结 12 927
[愿得一人]
[愿得一人] 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 20:47

    As answered by FreeAsInBeer, you can define this constant in .pch file. You may do like this in .pch file.

    // include Lumberjack header file 
    #import <Lumberjack/Lumberjack.h>
    
    // define ddLogLevel constant
    static const int ddLogLevel = LOG_LEVEL_VERBOSE;
    

    Im my implement, I create a new header file(e.g. mylog.h) for custom Lumberjack settings. in this way, I use #import statement in my .pch file for including mylog.h. This custom header file may like this.

    // include Lumberjack header file
    #import "Lumberjack.h" 
    
    #undef ZEKit_LOG_LEVEL
    #if defined (DEBUG) && (DEBUG == 1)
    #define ZEKit_LOG_LEVEL LOG_LEVEL_VERBOSE
    #else
    #define ZEKit_LOG_LEVEL LOG_LEVEL_WARN
    #endif
    
    static const int ddLogLevel = ZEKit_LOG_LEVEL;
    
    // ... Other custom settings
    
    0 讨论(0)
  • 2020-12-23 20:48

    I didn't find a better way to do it than the one explained in the article I mentioned in the question.

    Constant.h

    extern int const ddLogLevel;
    

    Constant.m

    #import "Constants.h"
    #import "DDLog.h"
    
    int const ddLogLevel = LOG_LEVEL_VERBOSE;
    

    Logger configuration

    #import "DDLog.h"
    #import "DDASLLogger.h"
    #import "DDTTYLogger.h"
    #import "DDFileLogger.h"
    
    ...
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
     [DDLog addLogger:[DDASLLogger sharedInstance]];
     [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
     DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; 
     [DDLog addLogger:fileLogger];
     [fileLogger release];
    
    ...
    

    Import your class

    #import "DDLog.h"
    #import "Constants.h"
    
    ...
    
    - (void)someMethod {
     DDLogVerbose(@"Log this message");
    }
    
    0 讨论(0)
  • 2020-12-23 20:51

    Share my configuration for CocoaLumberjack 2.0.0 with global log level and optional local log level with preserved DynamicLogLevels feature.

    My solution includes simple header file DSLogging.h (and it's counterpart) that import CocoaLumberjack.h and define convenience macros for setting up the files that use CocoaLumberjack log macros. Here is how you should use it:

    1. Import DSLogging.h header (two ways):
      • Import it in each file that uses CocoaLumberjack. Just as native framework documentation propose.
      • Import it in .pch file once. Consider this before going this way.
    2. Use DSLogLevelSetup... macros to set log level for file. Note: there should be macros in EACH source file that uses logging.

    See documentation inside for more details. Download gist.

    DSLogging.h header:

    //
    //  Created by DanSkeel on 23.04.15.
    
    #import "CocoaLumberjack.h"
    
    #define DS_LogScopeGlobal extern
    #define DS_LogScopeLocal static
    #define DS_LogMutableYes
    #define DS_LogMutableNo const
    
    #define DS_LogValueGlobal ;
    #define DS_LogValueLocal(lvl) = lvl
    
    #define DS_Setup_Log(scope, mutablility, value) scope mutablility DDLogLevel ddLogLevel value
    
    /** To setup loggin enviroment for particular file use one of these macros
     *
     *  @note Use CocoaLumberjack as usual (https://github.com/CocoaLumberjack/CocoaLumberjack/blob/master/Documentation/GettingStarted.md):
     *
     *  1. just import DSLoggin.h in source file instead of CocoaLumberjack.h
     *
     *  2. Use one of these macros to setup loggin enviroment for the file.
     *  Note: there should one of these macros in EACH file that uses CocoaLumberjack macroses.
     *  @example To enable logging for file with globally defined level you can make convinient snippet:
     *  @code
     *  #import "DSLogging.h"
     *  DSLogLevelSetupGlobal
     *  @endcode
     * 
     *  Use @b SetupGlobal to setup files that will use global level from @p DSLogging.m file
     *
     *  Use @b SetupMutableGlobal to be able to change global level at runtime (assign new level to ddLogLevel variable)
     *
     *  Use @b Setup(DDLogLevel) to set local log level
     *
     *  Use @b SetupMutable(DDLogLevel) to be able to modify local level at runtime ((assign new level to ddLogLevel variable))
     *
     *  This approach preserves a lot of CocoaLumberjack advantages. See SO https://stackoverflow.com/a/29837945/991816
     *
     *  @remarks details: these macros just help you define/reference ddLogLevel value. So if you
     *  see warning about <i> undeclared identifier </i> it should remind you to use one of these macros in this file.
     */
    extern char optionClickMeToSeePrettyDoc;
    #define DSLogLevelSetupMutableGlobal DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableYes, DS_LogValueGlobal)
    #define DSLogLevelSetupGlobal        DS_Setup_Log(DS_LogScopeGlobal, DS_LogMutableNo,  DS_LogValueGlobal)
    #define DSLogLevelSetupMutable(lvl)  DS_Setup_Log(DS_LogScopeLocal,  DS_LogMutableYes, DS_LogValueLocal(lvl))
    #define DSLogLevelSetup(lvl)         DS_Setup_Log(DS_LogScopeLocal,  DS_LogMutableNo,  DS_LogValueLocal(lvl))
    

    DSLogging.m source:

    //
    //  Created by DanSkeel on 23.04.15.
    
    #import "DSLogging.h"
    
    DDLogLevel ddLogLevel = DDLogLevelVerbose;
    

    Why I think it's a good approach:

    1. It's a little better than just CocoaLumberjack

      • Global level (can be mutable)
      • Allows you to "override" global level by local level (can be mutable)
    2. It doesn't cut CocoaLumberjack functions

      • Uses variable to set level, so it can be used with advanced features of CocoaLumberjack.

    I'm new to CocoaLumberjack and I can be too optimistic about my approach, would be glad to hear your critics if I lie at some point.

    0 讨论(0)
  • 2020-12-23 20:51

    There's an example app included with CocoaLumberjack that shows how to set a global log level that you can find here https://github.com/robbiehanson/CocoaLumberjack/tree/master/Xcode/GlobalLogLevel

    0 讨论(0)
  • 2020-12-23 20:53

    There is a much easier way to solve this, you can set the log level at the Logger instantiation:

    #ifdef DEBUG
      [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelDebug];
    #else
      [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelError];
    #endif
    

    So there is no need for extra imports or .pch-file.

    0 讨论(0)
  • 2020-12-23 20:55

    For those using CocoaLumberjackSwift you can simply set the following global variable anywhere in your code:

    dynamicLogLevel = .verbose
    

    Discussion here

    0 讨论(0)
提交回复
热议问题