Customizing the Log Level on iOS

You can change PSPDFKit’s log level using the logLevel property. It’s best to change the log level early on — for example, in application(_:willFinishLaunchingWithOptions:):

// Increase the log level to include debug messages.
PSPDFKit.SDK.shared.logLevel = .debug
// Increase the verbosity to include debug messages.
PSPDFKitGlobal.sharedInstance.logLevel = PSPDFLogLevelDebug;

The available log levels, in order of decreasing severity, are:

Setting a log level implies enabling all log levels with higher severity. For example, setting .warning also enables the logging of messages with .error or .critical severity.

Warning

Setting the log level to .debug or .verbose will have a significant performance impact on your app. We strongly recommend against using either of them in release builds.

To learn more about the different log levels, please take a look at LogLevel.

Custom Log Handler

By default, PSPDFKit uses OSLog to send logs to the operating system. You can override this behavior and use your own logging service instead of or in addition to OSLog:

PSPDFKit.SDK.shared.setLogHandler { level, tag, message, file, function, line in
    // Respect the configured log level.
    guard PSPDFKit.SDK.shared.logLevel >= level else {
        return
    }
    switch level {
        case .error:
            print("[PSPDFKit] Error in \(function): \(message())")
        default:
            print("[PSPDFKit] \(message())")
    }
}
[PSPDFKitGlobal.sharedInstance setLogHandler:^(PSPDFLogLevel level, const char *tag, NSString *(^message)(void), const char *file, const char *function, NSUInteger line) {
    // Respect the configured log level.
    if (PSPDFKitGlobal.sharedInstance.logLevel < level) {
        return
    }
    switch (level) {
        case PSPDFLogLevelError:
            NSLog(@"[PSPDFKit] Error in %s: %@", function, message());
            break;
        default:
            NSLog(@"[PSPDFKit] %@", message());
            break;
    }
}];

In this way, you can also report critical logs to your analytics or crash reporting service to gain more insight into which logs your users are hitting. You may also capture the current stack trace and attach it to the service you’re using:

case .critical:
    print("[PSPDFKit] Critical error in \(function): \(message())")
    MyAnalyticsService.logCriticalEvent(attributes: [
        "source": "\(file)/\(function)/\(line)",
        "message": message(),
        // Capture the stack trace.
        "stack_trace": Thread.callStackSymbols.joined(separator: "\n")
    ])
case PSPDFLogLevelCritical:
    NSLog(@"[PSPDFKit] Critical error in %s: %@", function, message());
    [MyAnalyticsService logCriticalEventWithAttributes:@{
        @"source": [NSString stringWithFormat:@"%s/%s/%tu", file, function, line],
        @"message": message(),
        // Capture the stack trace.
        @"stack_trace": [NSThread.callStackSymbols componentsJoinedByString:@"\n"]
    }];

To learn more about customizing the log handler and using custom analytics services, check out the API reference for setLogHandler(_:) and our Adding Logging to Crash Reports blog post.