Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Implemented different logLevel per each logger as suggested by @rivera-ernesto #151

Merged
merged 2 commits into from

5 participants

Bogdan Poplauschi Ernesto Rivera dstd Dmytro Vorobiov mickeysox
Bogdan Poplauschi
Collaborator
  • addLogger method new method with logLevel param
  • filtering via DDLoggerNode logLevel property
  • actual filtering inside lt_log
Bogdan Poplauschi bpoplauschi Implemented different logLevel per each logger as suggested by @river…
…a-ernesto

- addLogger method new method with logLevel param
- filtering via DDLoggerNode logLevel property
- actual filtering inside lt_log
6fa2b6f
Ernesto Rivera
Collaborator

I would like to merge this. Do you think you can do that small change to bpoplauschi@4310464#diff-cc127bbf3886b69e2c4a46bae5fff15bL185?

Bogdan Poplauschi
Collaborator

@rivera-ernesto I was making the change right now. Will be there in 2 minutes.

Bogdan Poplauschi
Collaborator

@rivera-ernesto It's updated. The previous solution was not wrong because inside addLogger: I was using the internal method lt_addLogger:logLevel:. But it order to keep the code clean, I made the update. Please review

Ernesto Rivera rivera-ernesto merged commit 24f567c into from
Ernesto Rivera
Collaborator

Nice. 1.6.3 should be close.

Bogdan Poplauschi bpoplauschi deleted the branch
dstd

What is the reason to use separate array filteredLoggers ?
Isn't it better to filter in-place, like this:

                for (DDLoggerNode *loggerNode in loggers)
                {
                        if (logMessage->logFlag > loggerNode.logLevel)
                                continue;
                        dispatch_<.....>

Without any prefiltering.

Ernesto Rivera
Collaborator

You're right and filteredLoggers is only used once. Could you make a pull request?

mickeysox

Hi,

Not sure if this is the correct place to raise this, but I could you check if 632 is causing an issue with XMPPLogging.h - explanation below.

The problem seems to be that the logFlag for XMPP_LOG_FLAG_SEND is always higher than the logLevel in

(void)lt_log:(DDLogMessage *)logMessage { ... if (logMessage->logFlag > loggerNode.logLevel) //see explanation below continue; ... }
So the log messages are skipped.

I'll try to explain...

In my XMPPStream.m I have defined:

#if DEBUG
static const int xmppLogLevel = XMPP_LOG_FLAG_SEND; // | XMPP_LOG_FLAG_TRACE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif

In XMPPLogging.h, XMPP_LOG_FLAG_SEND is pre-defined as:

#define XMPP_LOG_FLAG_SEND (1 << 5) //32

In DDLog.m

A logger with a log level of LOG_LEVEL_VERBOSE, which is defined:

#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

#define LOG_LEVEL_VERBOSE (LOG_FLAG_ERROR | LOG_FLAG_WARN | LOG_FLAG_INFO | LOG_FLAG_DEBUG | LOG_FLAG_VERBOSE) // 0...11111

And then added via this function:

(void)addLogger:(id )logger { [self addLogger:logger withLogLevel:LOG_LEVEL_VERBOSE]; NSLog(@"LOG_LEVEL_VERBOSE = %d", LOG_LEVEL_VERBOSE); }
This gives a total logLevel of 31.

Now in XMPPLogging.h we define XMPPLogSend as:

#define XMPPLogSend(format, ...) XMPP_LOG_OBJC_MAYBE(XMPP_LOG_ASYNC_SEND, xmppLogLevel, \
XMPP_LOG_FLAG_SEND, XMPP_LOG_CONTEXT, format, ##VA_ARGS)

Which sends XMPP_LOG_FLAG_SEND which is defined as 32.

So when the below function is called, the logFlag (32) is never greater than the logLevel (31)

(void)lt_log:(DDLogMessage *)logMessage { ... if (logMessage->logFlag > loggerNode.logLevel) //see explanation below continue; ... }
Hope that makes sense.

Thanks

Michael

Collaborator

I've copied question to issue #196.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2013
  1. Bogdan Poplauschi

    Implemented different logLevel per each logger as suggested by @river…

    bpoplauschi authored
    …a-ernesto
    
    - addLogger method new method with logLevel param
    - filtering via DDLoggerNode logLevel property
    - actual filtering inside lt_log
  2. Bogdan Poplauschi
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 14 deletions.
  1. +8 −2 Lumberjack/DDLog.h
  2. +31 −12 Lumberjack/DDLog.m
10 Lumberjack/DDLog.h
View
@@ -323,9 +323,15 @@ NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
* you should create and add a logger.
**/
-+ (void)addLogger:(id <DDLogger>)logger;
-+ (void)removeLogger:(id <DDLogger>)logger;
++ (void)addLogger:(id <DDLogger>)logger; // adds the logger using maximum log level (LOG_LEVEL_VERBOSE)
+
+/**
+ * Please use as logLevels the LOG_LEVEL_* macros
+ *
+**/
++ (void)addLogger:(id <DDLogger>)logger withLogLevel:(int)logLevel;
++ (void)removeLogger:(id <DDLogger>)logger;
+ (void)removeAllLoggers;
/**
43 Lumberjack/DDLog.m
View
@@ -59,16 +59,19 @@ @interface DDLoggerNode : NSObject {
@public
id <DDLogger> logger;
dispatch_queue_t loggerQueue;
+ int logLevel;
}
-+ (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue;
+@property (nonatomic, assign, readonly) int logLevel;
+
++ (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue logLevel:(int)logLevel;
@end
@interface DDLog (PrivateAPI)
-+ (void)lt_addLogger:(id <DDLogger>)logger;
++ (void)lt_addLogger:(id <DDLogger>)logger logLevel:(int)logLevel;
+ (void)lt_removeLogger:(id <DDLogger>)logger;
+ (void)lt_removeAllLoggers;
+ (void)lt_log:(DDLogMessage *)logMessage;
@@ -180,11 +183,16 @@ + (void)applicationWillTerminate:(NSNotification *)notification
+ (void)addLogger:(id <DDLogger>)logger
{
- if (logger == nil) return;
-
+ [self addLogger:logger withLogLevel:LOG_LEVEL_VERBOSE];
+}
+
++ (void)addLogger:(id <DDLogger>)logger withLogLevel:(int)logLevel
+{
+ if (logger == nil) return;
+
dispatch_async(loggingQueue, ^{ @autoreleasepool {
- [self lt_addLogger:logger];
+ [self lt_addLogger:logger logLevel:logLevel];
}});
}
@@ -504,7 +512,7 @@ + (void)setLogLevel:(int)logLevel forClassWithName:(NSString *)aClassName
/**
* This method should only be run on the logging thread/queue.
**/
-+ (void)lt_addLogger:(id <DDLogger>)logger
++ (void)lt_addLogger:(id <DDLogger>)logger logLevel:(int)logLevel
{
// Add to loggers array.
// Need to create loggerQueue if loggerNode doesn't provide one.
@@ -532,7 +540,7 @@ + (void)lt_addLogger:(id <DDLogger>)logger
loggerQueue = dispatch_queue_create(loggerQueueName, NULL);
}
- DDLoggerNode *loggerNode = [DDLoggerNode nodeWithLogger:logger loggerQueue:loggerQueue];
+ DDLoggerNode *loggerNode = [DDLoggerNode nodeWithLogger:logger loggerQueue:loggerQueue logLevel:logLevel];
[loggers addObject:loggerNode];
if ([logger respondsToSelector:@selector(didAddLogger)])
@@ -612,6 +620,14 @@ + (void)lt_removeAllLoggers
+ (void)lt_log:(DDLogMessage *)logMessage
{
// Execute the given log message on each of our loggers.
+
+ // filter the loggers that should write this message based on the logLevel
+ NSMutableArray *filteredLoggers = [NSMutableArray arrayWithCapacity:loggers.count];
+ for (DDLoggerNode *loggerNode in loggers) {
+ if (logMessage->logFlag <= loggerNode.logLevel) {
+ [filteredLoggers addObject:loggerNode];
+ }
+ }
if (numProcessors > 1)
{
@@ -622,7 +638,7 @@ + (void)lt_log:(DDLogMessage *)logMessage
// The waiting ensures that a slow logger doesn't end up with a large queue of pending log messages.
// This would defeat the purpose of the efforts we made earlier to restrict the max queue size.
- for (DDLoggerNode *loggerNode in loggers)
+ for (DDLoggerNode *loggerNode in filteredLoggers)
{
dispatch_group_async(loggingGroup, loggerNode->loggerQueue, ^{ @autoreleasepool {
@@ -637,7 +653,7 @@ + (void)lt_log:(DDLogMessage *)logMessage
{
// Execute each logger serialy, each within its own queue.
- for (DDLoggerNode *loggerNode in loggers)
+ for (DDLoggerNode *loggerNode in filteredLoggers)
{
dispatch_sync(loggerNode->loggerQueue, ^{ @autoreleasepool {
@@ -773,7 +789,9 @@ + (void)lt_flush
@implementation DDLoggerNode
-- (instancetype)initWithLogger:(id <DDLogger>)aLogger loggerQueue:(dispatch_queue_t)aLoggerQueue
+@synthesize logLevel;
+
+- (instancetype)initWithLogger:(id <DDLogger>)aLogger loggerQueue:(dispatch_queue_t)aLoggerQueue logLevel:(int)aLogLevel
{
if ((self = [super init]))
{
@@ -785,13 +803,14 @@ - (instancetype)initWithLogger:(id <DDLogger>)aLogger loggerQueue:(dispatch_queu
dispatch_retain(loggerQueue);
#endif
}
+ logLevel = aLogLevel;
}
return self;
}
-+ (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue
++ (DDLoggerNode *)nodeWithLogger:(id <DDLogger>)logger loggerQueue:(dispatch_queue_t)loggerQueue logLevel:(int)logLevel
{
- return [[DDLoggerNode alloc] initWithLogger:logger loggerQueue:loggerQueue];
+ return [[DDLoggerNode alloc] initWithLogger:logger loggerQueue:loggerQueue logLevel:logLevel];
}
- (void)dealloc
Something went wrong with that request. Please try again.