CocoaLumberjack Custom Log Levels

2.1k views Asked by At

I've started using CocoaLumberjack and was interested in using their custom log levels feature. They have a handy config available at https://github.com/CocoaLumberjack/CocoaLumberjack/wiki/CustomLogLevels to get you started. I don't know exactly what I want right now, so I left it as is.

Next I set my debug level in my code using this...

static const int ddLogLevel = LOG_LEVEL_DEBUG;

However with this set up it appears only messages of GREATER severity than LOG_LEVEL_DEBUG get presented. Meaning calls to DDLogInfo() and above show up, but not DDLogDebug(). This is also true as I slide upwards. So...

static const int ddLogLevel = LOG_LEVEL_INFO;

...ignores DDLogInfo() and DDLogDebug(), but shows for DDLogNotice() and higher. Surely the expected behavior is to be inclusive of that warning level.

Here's the code deep inside CocoaLumberjack that makes the decision...

for (DDLoggerNode *loggerNode in loggers) {
    // skip the loggers that shouldn't write this message based on the logLevel

    if (!(logMessage->logFlag & loggerNode.logLevel)) {
        continue;
    }

    dispatch_group_async(loggingGroup, loggerNode->loggerQueue, ^{ @autoreleasepool {

        [loggerNode->logger logMessage:logMessage];

    }});
}

In my first example, my log level is 111111 (63) and message flag (using DDLogDebug()) is 100000 (32). 63 & 32 is YES, so this fails (with the NOT). So I would think the message would get logged. Moving the log method up to DDLogInfo() which has a message flag of 010000 (16). This is still YES and therefore fails with the NOT, so we get logged. But I see it in this case. Anyone have any experience with this?

2

There are 2 answers

3
James Frost On

I think I've worked it out. In CocoaLumberjack itself, in DDLog.h, the log level flags are defined like so:

#define LOG_FLAG_ERROR    (1 << 0)  // 0...00001
#define LOG_FLAG_WARN     (1 << 1)  // 0...00010
#define LOG_FLAG_INFO     (1 << 2)  // 0...00100
#define LOG_FLAG_DEBUG    (1 << 3)  // 0...01000
#define LOG_FLAG_VERBOSE  (1 << 4)  // 0...10000

However, the CustomLogLevels MyLog.h file defines them like so:

#define LOG_FLAG_FATAL   (1 << 0)  // 0...000001
#define LOG_FLAG_ERROR   (1 << 1)  // 0...000010
#define LOG_FLAG_WARN    (1 << 2)  // 0...000100
#define LOG_FLAG_NOTICE  (1 << 3)  // 0...001000
#define LOG_FLAG_INFO    (1 << 4)  // 0...010000
#define LOG_FLAG_DEBUG   (1 << 5)  // 0...100000

Note that it adds an extra FATAL flag, which has the effect of shifting all of the other flags down a place. This is the cause of the issue you were seeing.

0
DDP On

if you have custom levels higher than the inbuilt, try initialising with:

[DDLog addLogger: [DDASLLogger sharedInstance] withLogLevel: LOG_LEVEL_CUSTOM];
[DDLog addLogger: [DDTTYLogger sharedInstance] withLogLevel: LOG_LEVEL_CUSTOM];

where LOG_LEVEL_CUSTOM is defined in your MyLog.h as:

#define LOG_LEVEL_CUSTOM (LOG_FLAG_ERROR | LOG_FLAG_WARN | LOG_FLAG_INFO | LOG_FLAG_CUSTOM)