iosobjective-cloggingcocoalumberjack

Global log level for CocoaLumberjack


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


Solution

  • 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");
    }