Logging

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

The default log level is [.info, .warning, .error, .critical]:

1
2
// Change log level to be more verbose.
PSPDFKit.SDK.shared.logLevel = [.info, .warning, .error, .critical, .verbose]
Copy
1
2
// Change log level to be more verbose.
PSPDFKitGlobal.sharedInstance.logLevel = PSPDFLogLevelMaskInfo|PSPDFLogLevelMaskWarning|PSPDFLogLevelMaskError|PSPDFLogLevelMaskCritical|PSPDFLogLevelMaskVerbose;

Available Log Levels:

If you want to learn more about the different log levels, please take a look at LogLevelMask.

Custom Log Handler

PSPDFKit also allows you to set a custom log handler to forward logging to a different system. By default, NSLog is used for logging. Here’s how you can set a different log handler:

Copy
1
2
3
4
5
6
7
8
PSPDFKit.SDK.shared.setLogHandler { level, tag, message, file, function, line in
    switch level {
    case .error:
        print("[PSPDFKit] Error in \(function): \(message())")
    default:
        print("[PSPDFKit] \(message())")
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
[PSPDFKitGlobal.sharedInstance setLogHandler:^(PSPDFLogLevelMask level, const char *tag, NSString *(^message)(void), const char *file, const char *function, NSUInteger line) {
    switch (level) {
        case PSPDFLogLevelMaskError:
            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 in order to gain more insight about which logs your users are hitting. You may also capture the current stack trace and attach it to the service you are using:

Copy
1
2
3
4
5
6
7
8
9
10
case .critical:
    print("[PSPDFKit] \(function): \(message())")

    let attributes = [
        "function_name": "\(function)/\(line)",
        "message": message(),
        // Capture the stack trace.
        "stack_trace": Thread.callStackSymbols.joined(separator: "\n")
    ]
    Analytics.logCriticalEvent(with: attributes)
Copy
1
2
3
4
5
6
7
8
9
10
case PSPDFLogLevelMaskCritical:
    NSLog(@"[PSPDFKit] %s: %@", function, message());

    NSDictionary<NSString *, NSString *> *attributes = @{
      @"function_name": [NSString stringWithFormat:@"%s/%tu", function, line],
      @"message": message(),
      // Capture the stack trace.
      @"stack_trace": [NSThread.callStackSymbols componentsJoinedByString:@"\n"]
    };
    [Analytics logCriticalEventWithAttributes:attributes];

See PSPDFKit.SDK.setLogHandler(handler:) for more information.