Global log level for CocoaLumberjack

2020-02-17 01:23发布

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 that bugs me: there doesn't seem to be an elegant way to define a log level for the whole app. To make it work I need to define a constant in every source file, like this:

static const int ddLogLevel = LOG_LEVEL_VERBOSE;

So, is there a way to define a global log level for the application?

I found this article on the subject, but I still need to add an #import in every file...

11条回答
【Aperson】
2楼-- · 2020-02-17 02:01

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

查看更多
仙女界的扛把子
3楼-- · 2020-02-17 02:09

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.

查看更多
冷血范
4楼-- · 2020-02-17 02:10

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):
  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.

查看更多
Melony?
5楼-- · 2020-02-17 02:12

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.

查看更多
闹够了就滚
6楼-- · 2020-02-17 02:12

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
查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-17 02:13

You could use an #include statement in your *.pch file so that it's automatically included in all your project's files.

查看更多
登录 后发表回答