NSLogger icon indicating copy to clipboard operation
NSLogger copied to clipboard

Allow thread information to be passed in externally (Integration w/ CocoaLumberjack)

Open tonyxiao opened this issue 9 years ago • 3 comments
trafficstars

When using CocoaLumberjack (or other logging frameworks) on top of NSLogger (see https://github.com/0xced/XCDLumberjackNSLogger/blob/develop/XCDLumberjackNSLogger.m), the threading information is no longer correct. screen shot 2015-11-29 at 3 25 32 pm

If we allow threading information to be passed in (DDLogMessage contains threadName info, http://cocoadocs.org/docsets/CocoaLumberjack/1.6/Classes/DDLogMessage.html#//api/name/threadID), it will make the NSLogger display much more useful when integrated with other logging frameworks.

tonyxiao avatar Nov 29 '15 23:11 tonyxiao

Good idea, although this will require yet another set of APIs

fpillet avatar Nov 30 '15 23:11 fpillet

@tonyxiao If you are using XCDLumberjackNSLogger 1.0.1 or later then you should have correct threading information. But of course, as mentioned in a comment, XCDLumberjackNSLogger abuses NSLogger’s thread name caching mechanism and a proper set of new APIs would be a better solution.

0xced avatar Dec 15 '15 00:12 0xced

Oh that's neat! I am not using XCDLumberjackNSLogger but I'll certainly be using a similar approach to hack around the issue for now.

Reference:

static void SetThreadNameWithMessage(DDLogMessage *logMessage)
{
    // There is no _thread name_ parameter for LogXXXToF functions, but we can abuse NSLogger’s thread name caching mechanism which uses the current thread dictionary
    NSString *queueLabel = [logMessage.queueLabel isEqualToString:@"com.apple.main-thread"] ? @"Main Queue" : logMessage.queueLabel;
    NSThread.currentThread.threadDictionary[@"__$NSLoggerThreadName$__"] = [NSString stringWithFormat:@"%@ [%@]", logMessage.threadID, queueLabel];
}

tonyxiao avatar Dec 15 '15 01:12 tonyxiao