The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Customize the Swift SDK logger

How to customize the log information about experiments coming from the Optimizely Feature Experimentation Swift SDK to help with debugging.

The logger logs information about your experiments to help you with debugging. You can customize where log information is sent and what kind of information is tracked.

In the Swift SDK, you can use our default logger and configure OptimizelyLogLevel. You can initialize it with any log level and pass it in when creating an OptimizelyClient instance. You can also implement your own logger and pass it in the builder when creating an OptimizelyClient instance. See the code examples below.

class CustomLogger: OPTLogger {
    public static var logLevel: OptimizelyLogLevel = .info

    required init() {
    }

    public func log(level: OptimizelyLogLevel, message: String) {
        if level.rawValue <= CustomLogger.logLevel.rawValue {
            print("\(message)")
        }
    }
}

// create OptimizelyClient with a custom Logger
let customLogger = CustomLogger()

optimizely = OptimizelyClient(sdkKey: "<Your_SDK_Key>",
                              logger: customLogger,
                              defaultLogLevel:.debug)
@interface CustomLogger : NSObject <OPTLogger>
+ (enum OptimizelyLogLevel)logLevel;
+ (void)setLogLevel:(enum OptimizelyLogLevel)value;
- (nonnull instancetype)init;
- (void)logWithLevel:(enum OptimizelyLogLevel)level message:(NSString *)message;
@end
  
@implementation CustomLogger
-(instancetype)init {
    self = [super init];
    if (self != nil) {
        //
    }
    
    return self;
}

- (void)logWithLevel:(enum OptimizelyLogLevel)level message:(NSString * _Nonnull)message {
    if (level <= CustomLogger.logLevel) {
        NSLog(@"%@", message);
    }
}

static enum OptimizelyLogLevel logLevel = OptimizelyLogLevelInfo;
+(enum OptimizelyLogLevel)logLevel {
    return logLevel;
}
+(void)setLogLevel:(enum OptimizelyLogLevel)value {
    logLevel = value;
}
@end


// create OptimizelyClient with a custom Logger
CustomLogger *customLogger = [[CustomLogger alloc] init];
     
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:@"<Your_SDK_Key>"
                            				logger:customLogger
                            				eventDispatcher:nil
                            				userProfileService:nil
                            				periodicDownloadInterval:5*60
                            				defaultLogLevel:OptimizelyLogLevelInfo];

Log levels

The table below lists the log levels for the SDK:

Log LevelExplanation
OptimizelyLogLevel.errorEvents that prevent feature flags from functioning correctly (for example, invalid datafile in initialization and invalid flag keys) are logged. The user can take action to correct.
OptimizelyLogLevel.warningEvents that do not prevent feature flags from functioning correctly, but can have unexpected outcomes (for example, future API deprecation, logger or error handler are not set properly, and nil values from getters) are logged.
OptimizelyLogLevel.infoEvents of significance (for example, decision started, decision succeeded, tracking started, and tracking succeeded) are logged. This is helpful in showing the lifecycle of an API call.
OptimizelyLogLevel.debugAny information related to errors that can help us debug the issue (for example, the feature flag is not running, user is not included in the flag rule) are logged.

Source files

The language/platform source files containing the implementation for Swift is OptimizelyClient.swift.