ContextFilterAppDelegate.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #import "ContextFilterAppDelegate.h"
  2. #import <CocoaLumberjack/CocoaLumberjack.h>
  3. #import "MyContextFilter.h"
  4. #import "ThirdPartyFramework.h"
  5. // Log levels: off, error, warn, info, verbose
  6. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  7. @implementation ContextFilterAppDelegate
  8. @synthesize window;
  9. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  10. {
  11. // Our application adopts a "third party framework" which also uses the lumberjack framework.
  12. // We love this because it greatly improves our ability to observe, debug, and diagnose problems!
  13. //
  14. // Now sometimes we want to see the third party framework log messages in our Xcode console.
  15. // And sometimes we don't (for whatever reason).
  16. //
  17. // We can accomplish this with the use of log message contexts.
  18. // Each log message has an associated context.
  19. // The context itself is simply an integer, and the default context is zero.
  20. // However, third party frameworks that employ lumberjack will likely use a custom non-zero context.
  21. //
  22. // For example, the CocoaHTTPServer project defines it's own internal log statments:
  23. //
  24. // HTTPLogWarn(@"File not found - %@", filePath);
  25. //
  26. // As part of its logging setup, it defines its own custom logging context:
  27. //
  28. // #define HTTP_LOG_CONTEXT 80
  29. //
  30. // And each HTTPLog message uses this HTTP_LOG_CONTEXT instead of the default context.
  31. // This means that we can tell if log messages are coming from our code or from the framework.
  32. // We're going to tap into this ability to filter out log messages from our "third party framework".
  33. // We want all log messages (from our code or the third party framework) to show up in the Xcode console.
  34. [DDLog addLogger:[DDTTYLogger sharedInstance]];
  35. // If we want to filter log messages from the third party framework, we can do this:
  36. MyContextFilter *filter = [[MyContextFilter alloc] init];
  37. [[DDTTYLogger sharedInstance] setLogFormatter:filter];
  38. // Now start up a timer to create some fake log messages for this example
  39. [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fire:) userInfo:nil repeats:YES];
  40. // Start the third party framework,
  41. // which will create a similar fake timer.
  42. [ThirdPartyFramework start];
  43. }
  44. - (void)fire:(NSTimer *)timer
  45. {
  46. DDLogVerbose(@"Log message from our code");
  47. }
  48. @end