lukas
lukas

Reputation: 2410

CocoaLumberjack different LogLevels for Debug and Relase not working

I'm developing an iOS Application using the CocoaLumberjack logging framework. On this wiki site is an article on how to use automatically different Log Levels for debug and relase. I implemented the code like the following:

#import "TableViewController.h"
#import "DDLog.h"

@interface TableViewController ()

@end


#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif

@Implementation TableViewController

But it doesn't work and if I'm running the relase configuration, it still logs verbose logs as well.

I'm using nearly always Verbose logs which I call like this:

DDLogVerbose(@"Some log...");

I'm using CocoaLumberjack in every Class and I have the same LogLevel implementation in all the other .m files.

Upvotes: 0

Views: 1569

Answers (2)

lance-ios
lance-ios

Reputation: 41

Inspired by @greymouser 's answer i put together the following preprocessor macro to be included in the .pch file for the project, which allows the log level to be set on a per-file basis in debug mode whilst setting everything to a default level in other modes. Warning suppression is also included in the macro, to save lines of code in the class files themselves.

#undef  LOG_LEVEL_DEF // Undefine first only if needed
#define LOG_LEVEL_DEF ddLogLevel

#ifdef DEBUG
    #define DD_LOGGING_LEVEL(loggingLevel) _Pragma("clang diagnostic push") \
                                           _Pragma("clang diagnostic ignored \"-Wunused-variable\"") \
                                           static DDLogLevel ddLogLevel = loggingLevel \
                                            _Pragma("clang diagnostic push")

#else 
    #define DD_LOGGING_LEVEL(loggingLevel) _Pragma("clang diagnostic push") \
                                           _Pragma("clang diagnostic ignored \"-Wunused-variable\"") \
                                           static const DDLogLevel ddLogLevel = DDLogLevelError \
                                           _Pragma("clang diagnostic push")
#endif

Usage:

in the .m files:

DD_LOGGING_LEVEL(DDLogLevelVerbose);

Upvotes: 0

greymouser
greymouser

Reputation: 3181

First, can you update your queston to show what DDLog* calls you are using? That can help to confirm that you are using something that will actually get logged (or not) correctly.

Second, some of your example logs showing calls in release mode could help, too -- specially showing what class instances they are coming from.

So, are you only using DDLog in GeneratrTableViewController.m? What you have will set the ddLogLevel static const variable only in GeneratrTableViewController.m. (I'm assuming that's what I'm looking at, based on what the code looks like.) If you're using DDLog* calls in any other files, it'll use whatever the default log level is there.

UPDATE

Okay, your update helped me understand where you were at. You should not be setting it up like that for each class.

First, do it globally, add something like the following once to your precompiled header -- not every file, not ever header --

#ifdef DEBUG
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wunused-variable"
    static int ddLogLevel = LOG_LEVEL_DEBUG;
    #pragma clang diagnostic pop
#else
    static const int ddLogLevel = LOG_LEVEL_WARN;
#endif

The #pragma's to not warn on unused variables mask warnings that arise in the corner case where you're in DEBUG, but the variable happens not to be used because of lack of logging. The non-const in debug mode here is important -- if you want to override the level per class, you're going to need to be able to change it. But this has to happen dynamically.

For each class that you may want to override the logging setting in, you need something like the following (and still adding the above to your precompiled header).

+ (void)initialize
{
    [self ddSetLogLevel:LOG_LEVEL_VERBOSE];
}

+ (int)ddLogLevel
{
    return LOG_LEVEL_VERBOSE;
}

+ (void)ddSetLogLevel:(int)logLevel
{
    ddLogLevel = logLevel;
}

I tend to keep my global level at DEBUG (in DEBUG mode). Then I add the code directly above to classes when I'm actively developing them and use DDLogVerbose liberally. Then, when main dev is done, I wrap it in #if 0/#endif so I can easily add it back in later; changing the global setting would also work just fine.

Upvotes: 1

Related Questions