Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Customize logger

This topic describes how to customize the log information about experiments 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 Swift SDK.

Log LevelExplanation
OptimizelyLogLevel.errorEvents that prevent feature flags from functioning correctly (for example, invalid datafile in initialization and invalid feature 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, activate started, activate 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 rollout) are logged.