Customize the Swift SDK logger
Customize log information from the Optimizely Feature Experimentation Swift SDK for debugging experiments.
The logger captures details about your experiments, making it easier for you to debug them. You can customize where the log information is stored and choose the types of data you want to track.
In the Swift SDK, you can use the default logger and configure OptimizelyLogLevel
. Initialize it with any log level and pass it in when you create an OptimizelyClient
instance. You can also implement your own logger and pass it in the builder when creating an OptimizelyClient
instance. See the following code sample:
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 following list shows the log levels for the Swift SDK.
- OptimizelyLogLevel.error – Logs events that prevent feature flags from working properly, such as an invalid data file during initialization or incorrect feature keys. These issues require user intervention to fix.
- OptimizelyLogLevel.warning – Logs events that do not prevent feature flags from working properly but may cause unexpected results, such as future API deprecation, improper logger or error handler settings, and nil values from getters.
- OptimizelyLogLevel.info – Logs key events to illustrate the lifecycle of an API call, such as when a decision or tracking starts and succeeds.
- OptimizelyLogLevel.debug – Logs extra information related to errors that aid Optimizely in debugging, like when a feature flag is not running or a user is not included in a rollout.
Source files
The language or platform source files containing the implementation for Swift is OptimizelyClient.swift.
Updated 17 days ago