Cocoa Lumberjack: how to show file and line number?

后端 未结 3 1544
挽巷
挽巷 2021-02-06 07:18

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.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-06 07:54

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

    @interface LineNumberLogFormatter : NSObject
    
    - (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
    

提交回复
热议问题