iosobjective-cmacosdebuggingcocoalumberjack

Cocoa Lumberjack: how to show file and line number?


I am trying to find a way for Cocoa Lumberjack to show me file and line number.

After looking through the docs and some Googling, I found no easy way to do this.

Is there any way to do this without adding custom formatter?


Solution

  • Well, like I said, there is no built-in way. So, I've implemented custom formatter:

    @interface LineNumberLogFormatter : NSObject<DDLogFormatter>
    
    - (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
    
    @end
    
    @implementation LineNumberLogFormatter
    - (NSString *)formatLogMessage:(DDLogMessage *)logMessage
    {
        NSString *path = [NSString stringWithCString:logMessage->file encoding:NSASCIIStringEncoding];
        NSString *fileName = [path lastPathComponent];
        return [NSString stringWithFormat:@"%@:%d %@", fileName, logMessage->lineNumber, logMessage->logMsg];
    }
    @end