Logging

Logging Overview

Logs in the DarkMatter SDK are sent to the LogService class. By default these logs go nowhere. It is up to the app to register log handlers.

You can register any number of log handlers for each type of log message (verbose, info, warning, and error). You can control which types of messages are sent to the handlers by setting the LogService.verbosity property.

Note: The log service stores a weak reference to it’s handlers so make sure you have a strong reference to the object you pass in somewhere.

Log Handlers should be set up as early as possible to ensure no logs are missed. Ideally this would be done in your application(_: , didFinishLaunchingWithOptions:) method.

Setting up a log handler

This example will show you how you could set up a log handler in your application delegate class.

  • Import the DarkMatter module in your AppDelegate file.
  // Swift
  import DarkMatter
  // Objective-C
  @import DarkMatter;
  • Add a property to your AppDelegate class.
  // Swift
  class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    let logHandler = LogHandler({ (item: Any) -> Void in
      print(item)
    })
    // ...
  }
  // Objective-C
  @interface AppDelegate ()
  // ...
  @property LogHandler *logHandler;
  // ...
  @end

  @implementation AppDelegate
  // ...
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    self.logHandler = [[LogHandler alloc] init:^(id _Nonnull item) {
      NSLog(@"%@", item);
    }];
    // ...
  }
  @end
  • Set the verbosity level in your applicationDidFinishLaunchingWithOptions method.
  // Swift
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // ...
    LogService.verbosity = LogService.LogLevel.Verbose
    // ...
  }
  // Objective-C
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    LogService.verbosity = LogLevelVerbose;
    // ...
  }
  • Register your log handler in your applicationDidFinishLaunchingWithOptions method.
  // Swift
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // ...
    LogService.addLogVerboseHandler(logHandler)
    LogService.addLogHandler(logHandler)
    LogService.addLogWarningHandler(logHandler)
    LogService.addLogErrorHandler(logHandler)
    // ...
  }
  // Objective-C
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ...
    [LogService addLogVerboseHandler:self.logHandler];
    [LogService addLogHandler:self.logHandler];
    [LogService addLogWarningHandler:self.logHandler];
    [LogService addLogErrorHandler:self.logHandler];
    // ...
  }

The full code would look something like this:

// Swift
import DarkMatter
// ...
class AppDelegate: UIResponder, UIApplicationDelegate {
  // ...
  let logHandler = LogHandler({ (item: Any) -> Void in
    print(item)
  })
  // ...
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // ...
    LogService.verbosity = LogService.LogLevel.Verbose

    LogService.addLogVerboseHandler(logHandler)
    LogService.addLogHandler(logHandler)
    LogService.addLogWarningHandler(logHandler)
    LogService.addLogErrorHandler(logHandler)
    // ...
  }
}
// Objective-C
@import DarkMatter;
// ...
@interface AppDelegate ()
// ...
@property LogHandler *logHandler;
// ...
@end

@implementation AppDelegate
// ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // ...
  self.logHandler = [[LogHandler alloc] init:^(id _Nonnull item) {
    NSLog(@"%@", item);
  }];

  LogService.verbosity = LogLevelVerbose;

  [LogService addLogVerboseHandler:self.logHandler];
  [LogService addLogHandler:self.logHandler];
  [LogService addLogWarningHandler:self.logHandler];
  [LogService addLogErrorHandler:self.logHandler];
  // ...
}
// ...
@end

Now you can send a test message to the log service to make sure it is working. Add the following code in your view controller’s viewDidLoad method and you should see the test log in the console when you run your app.

// Swift
import DarkMatter
// ...
override func viewDidLoad() {
  // ...
  LogService.log("test log")
  // ...
}
// ...
// Objective-C
@import DarkMatter;
// ...
- (void)viewDidLoad {
  // ...
  [LogService log:@"test log"];
  // ...
}
// ...