0

What is the best way to include self in each log message in CocoaLumberjack?

What I tried:

  • Implementing the DDLogFormatter protocol - the caller of the log message is not available in the formatLogMessage: method.
  • #defineing something like:

    #define LogInfo(frmt, ...) DDLogInfo(([NSString stringWithFormat:@"%@: %@", self, frmt]), ##__VA_ARGS__) (note the extra () around the [NSString ...] - thanks hamstergene

    which will create a problem whenever I want to reference self weakly, e.g. calling LogInfo inside a block - including self would potentially cause memory problems.

What am I missing? Any other better logging library?

Community
  • 1
  • 1
mllm
  • 17,068
  • 15
  • 53
  • 64
  • 2
    The second should work if you put an extra `()` around `[...]`. – hamstergene Mar 30 '16 at 14:32
  • That works, can you explain why the extra `()` helped? – mllm Mar 31 '16 at 11:40
  • 2
    C preprocessor does not recognize `[]` as brackets, the commas inside them confuse it to think it's three separate macro arguments. – hamstergene Mar 31 '16 at 11:48
  • 1
    Be very careful about using that macro within blocks -- that can create a hidden strong reference to self, which in some situations can create a retain loop. When I've done something like this, I make a second logging macro which takes an object argument explicitly, so you can pass in a weakSelf if necessary. With a custom macro, you might be able to pass the object as the tag, and a special context value to know it's the logging object, if you want to do something special in the formatter. – Carl Lindberg May 03 '16 at 02:30

0 Answers0