ContextFilterAppDelegate.m 2.5 KB

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