Selective Logging For Cocoa
Does anyone have any good ideas around selective logging for Cocoa? I've been trying to think of easy ways to turn on and off groups of classes (say, my service classes) at a time. However, I'd like to figure out a way that doesn't force me into levels of logging. I've looked around and it seems that there is 开发者_运维问答no good logging frameworks for Cocoa, except maybe for the log4 port. Any ideas would be appreciated.
I've found Cocoa Lumberjack is an excellent library. It's really easy to integrate into your project and also highly customizable. It also supports GCD and has very high performance compared to NSLog. The documents are all in the wiki pages.
There's several different ways to achieve what you want with lumberjack as you can control output at the class/file level and do custom 'context' level filtering.
It does use "levels" by default, but it doesn't force you to use it. You can just do calls to the underlying LOG macros instead of the default level macros like ERROR, WARN, INFO, etc. You can also redefine levels to suit.
There's also a github repo.
If you're willing to rebuild your application after each change of logging settings, you can look at Three20's TTDebug.h and TTDebugFlags.h for example.
It works in this way - if DEBUG macro is defined, TTDPRINT macro is also defined and same applies to TTCONDITIONLOG macro. If DEBUG macro is not defined, you can still use TTDPRINT macro, but it does nothing. TTDCONDITIONLOG can be used in this way:
TTDCONDITIONLOG( DEBUG_MY_SERVICES, @"Hey, my debug message" );
To enable debug messages, define DEBUG macro. If you would like to enable debug messages of your services, just define also DEBUG_MY_SERVICES as:
#define DEBUG_MY_SERVICES 1
And if you do want to switch your services debugging info off, change 1 to 0.
It's easy, it's fast and it's enough for most situations.
精彩评论