Skip to content

Commit b70cc9e

Browse files
chore: add new classes for logging
- Add FlutterLogbackAppender.java for logging events in the Flutter app - Integrate OptimizelyFlutterSdkPlugin with FlutterLogbackAppender - Update Constants.swift, SwiftOptimizelyFlutterSdkPlugin.swift, and related classes
1 parent 00fd215 commit b70cc9e

File tree

7 files changed

+77
-22
lines changed

7 files changed

+77
-22
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.optimizely.optimizely_flutter_sdk;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import ch.qos.logback.classic.spi.ILoggingEvent;
10+
import ch.qos.logback.core.AppenderBase;
11+
import io.flutter.plugin.common.MethodChannel;
12+
13+
public class FlutterLogbackAppender extends AppenderBase<ILoggingEvent> {
14+
15+
public static final String CHANNEL_NAME = "optimizely_flutter_sdk_logger";
16+
public static MethodChannel channel;
17+
private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper());
18+
19+
public static void setChannel(MethodChannel channel) {
20+
FlutterLogbackAppender.channel = channel;
21+
}
22+
23+
@Override
24+
protected void append(ILoggingEvent event) {
25+
if (channel == null) {
26+
return;
27+
}
28+
29+
String message = event.getFormattedMessage();
30+
int level = event.getLevel().toInt();
31+
32+
Map<String, Object> logData = new HashMap<>();
33+
logData.put("level", level);
34+
logData.put("message", message);
35+
36+
mainThreadHandler.post(() -> {
37+
if (channel != null) {
38+
channel.invokeMethod("log", logData);
39+
}
40+
});
41+
}
42+
}

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@
3232

3333
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
3434

35+
import org.slf4j.LoggerFactory;
36+
37+
import ch.qos.logback.classic.Logger;
38+
import ch.qos.logback.classic.LoggerContext;
39+
import ch.qos.logback.classic.spi.ILoggingEvent;
40+
import ch.qos.logback.core.Appender;
41+
42+
3543
/** OptimizelyFlutterSdkPlugin */
3644
public class OptimizelyFlutterSdkPlugin extends OptimizelyFlutterClient implements FlutterPlugin, ActivityAware, MethodCallHandler {
3745

3846
public static MethodChannel channel;
47+
private Appender<ILoggingEvent> flutterLogbackAppender;
3948

4049
@Override
4150
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
@@ -157,11 +166,31 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
157166
channel = new MethodChannel(binding.getBinaryMessenger(), "optimizely_flutter_sdk");
158167
channel.setMethodCallHandler(this);
159168
context = binding.getApplicationContext();
169+
170+
MethodChannel loggerChannel = new MethodChannel(binding.getBinaryMessenger(), FlutterLogbackAppender.CHANNEL_NAME);
171+
FlutterLogbackAppender.setChannel(loggerChannel);
172+
173+
// Add appender to logback
174+
flutterLogbackAppender = new FlutterLogbackAppender();
175+
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
176+
flutterLogbackAppender.setContext(lc);
177+
flutterLogbackAppender.start();
178+
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
179+
rootLogger.addAppender(flutterLogbackAppender);
160180
}
161181

162182
@Override
163183
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
164184
channel.setMethodCallHandler(null);
185+
// Stop and detach the appender
186+
if (flutterLogbackAppender != null) {
187+
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
188+
rootLogger.detachAppender(flutterLogbackAppender);
189+
flutterLogbackAppender.stop();
190+
flutterLogbackAppender = null;
191+
}
192+
// Clean up the channel
193+
FlutterLogbackAppender.setChannel(null);
165194
}
166195

167196
@Override

ios/Classes/HelperClasses/Constants.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ struct RequestParameterKey {
9191
static let reasons = "reasons"
9292
static let decideOptions = "optimizelyDecideOption"
9393
static let defaultLogLevel = "defaultLogLevel"
94-
static let useCustomLogger = "useCustomLogger"
9594
static let eventBatchSize = "eventBatchSize"
9695
static let eventTimeInterval = "eventTimeInterval"
9796
static let eventMaxQueueSize = "eventMaxQueueSize"

ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,10 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
172172
notificationIdsTracker.removeValue(forKey: sdkKey)
173173
optimizelyClientsTracker.removeValue(forKey: sdkKey)
174174

175-
// Check if custom logger is requested
176-
var logger: OPTLogger?
177-
if let useCustomLogger = parameters[RequestParameterKey.useCustomLogger] as? Bool, useCustomLogger {
178-
// OptimizelyFlutterLogger bridges iOS logs to Flutter via Method Channel
179-
// When useCustomLogger = true:
180-
// iOS SDK log → OptimizelyFlutterLogger → Flutter Method Channel → Flutter console
181-
logger = OptimizelyFlutterLogger()
182-
}
183-
175+
// OptimizelyFlutterLogger bridges iOS logs to Flutter via Method Channel
176+
// iOS SDK log → OptimizelyFlutterLogger → Flutter Method Channel → Flutter console
177+
var logger: OPTLogger = OptimizelyFlutterLogger()
178+
184179
// Creating new instance
185180
let optimizelyInstance = OptimizelyClient(
186181
sdkKey:sdkKey,

lib/optimizely_flutter_sdk.dart

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ class OptimizelyFlutterSdk {
7373
final OptimizelyLogLevel _defaultLogLevel;
7474
final SDKSettings _sdkSettings;
7575
static OptimizelyLogger? _customLogger;
76-
/// Set a custom logger for the SDK
77-
static void setLogger(OptimizelyLogger logger) {
78-
_customLogger = logger;
79-
LoggerBridge.initialize(logger);
80-
}
8176
/// Get the current logger
8277
static OptimizelyLogger? get logger {
8378
return _customLogger;
@@ -97,11 +92,8 @@ class OptimizelyFlutterSdk {
9792
_defaultLogLevel = defaultLogLevel,
9893
_sdkSettings = sdkSettings {
9994
// Set the logger if provided
100-
if (logger != null) {
101-
setLogger(logger);
102-
} else {
103-
logWarning("Logger not provided.");
104-
}
95+
_customLogger = logger ?? DefaultOptimizelyLogger();
96+
LoggerBridge.initialize(_customLogger);
10597
}
10698

10799
/// Starts Optimizely SDK (Synchronous) with provided sdkKey.

lib/src/optimizely_client_wrapper.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ class OptimizelyClientWrapper {
8080
Constants.eventBatchSize: eventOptions.batchSize,
8181
Constants.eventTimeInterval: eventOptions.timeInterval,
8282
Constants.eventMaxQueueSize: eventOptions.maxQueueSize,
83-
Constants.useCustomLogger: logger != null,
8483
};
8584

8685
// Odp Request params

lib/src/utils/constants.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class Constants {
8686
static const String optimizelyDecideOption = "optimizelyDecideOption";
8787
static const String optimizelySegmentOption = "optimizelySegmentOption";
8888
static const String optimizelySdkSettings = "optimizelySdkSettings";
89-
static const String useCustomLogger = 'useCustomLogger';
9089
static const String defaultLogLevel = "defaultLogLevel";
9190
static const String payload = "payload";
9291
static const String value = "value";

0 commit comments

Comments
 (0)