Cocoa Lumberjack: how to show file and line number?

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

Looking through documents and some Google, I did not find an easy way to do this.

Is there a way to do this without adding custom formatting?

+7
debugging ios objective-c macos lumberjack
source share
4 answers

Well, as I said, there is no built-in way. So, I implemented a 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 
+15
source share

While a separate formatting class will work, it will make your logging code a bit more verbose. In my project, I decided to add some additional macros that use CocoaLumberjack as follows:

 // Prefix.pch file // ... #ifdef DEBUG #define DLogError(fmt, ...) DDLogError((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #define DLogWarn(fmt, ...) DDLogWarn((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #define DLogInfo(fmt, ...) DDLogInfo((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #define DLogDebug(fmt, ...) DDLogDebug((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #define DLogVerbose(fmt, ...) DDLogVerbose((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__) #else #define DLogError(fmt, ...) #define DLogWarn(fmt, ...) #define DLogInfo(fmt, ...) #define DLogDebug(fmt, ...) #define DLogVerbose(fmt, ...) #endif 

In the client code, you can call:

 DLogWarn(@"This is a warning"); 
+7
source share

As UrK suggested, there is no trivial way, but it's pretty simple if you define your own formatter (see doc )

0
source share

Xcode Snippet Way:

  1. write the following in the file:
 [\(#file):\(#line)(\(#function))] 
  1. select this code, select Menu → Editor → Create code snippet,
  2. in the pop-up dialog box, set "completion area" = "line or comment", "completion label" = "logpos", "platform" = "all", "language" = "swift", "title" = "log position"
  3. Accept and close the dialog
  4. in your DDLogXXX() function, DDLogXXX() , for example. You write DDLogDebug("") , insert your caret into "" , press ESC, you can see the completion of your logpos.

The above code snippet is for the quick version> = 3, for the quick version <3 it should be

 [\(__FILE__):\(__LINE__)(\(__FUNCTION__))] 
0
source share

All Articles