Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
public class FlutterLogbackAppender extends AppenderBase<ILoggingEvent> {

public static final String CHANNEL_NAME = "optimizely_flutter_sdk_logger";
public static MethodChannel channel;
private static MethodChannel channel;
private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

public static void setChannel(MethodChannel channel) {
FlutterLogbackAppender.channel = channel;
public static void setChannel(MethodChannel newChannel) {
if (channel == null) {
channel = newChannel;
}
}

public static void clearChannel() {
channel = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
if (channel != null) {
return;
}
channel = new MethodChannel(binding.getBinaryMessenger(), "optimizely_flutter_sdk");
channel.setMethodCallHandler(this);
context = binding.getApplicationContext();
Expand All @@ -232,15 +235,16 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
channel = null;
// Stop and detach the appender
if (flutterLogbackAppender != null) {
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.detachAppender(flutterLogbackAppender);
flutterLogbackAppender.stop();
flutterLogbackAppender = null;
}
// Clean up the channel
FlutterLogbackAppender.setChannel(null);
// Clean up the logger channel
FlutterLogbackAppender.clearChannel();
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion ios/Classes/OptimizelyFlutterLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public class OptimizelyFlutterLogger: NSObject, OPTLogger {
}

public static func setChannel(_ channel: FlutterMethodChannel) {
loggerChannel = channel
if loggerChannel == nil {
loggerChannel = channel
}
}

public static func clearChannel() {
loggerChannel = nil
}

public func log(level: OptimizelyLogLevel, message: String) {
Expand Down
11 changes: 10 additions & 1 deletion ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {

/// Registers optimizely_flutter_sdk channel to communicate with the flutter sdk to receive requests and send responses
public static func register(with registrar: FlutterPluginRegistrar) {
if channel != nil {
return
}
channel = FlutterMethodChannel(name: "optimizely_flutter_sdk", binaryMessenger: registrar.messenger())
let instance = SwiftOptimizelyFlutterSdkPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
Expand All @@ -50,7 +53,13 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
taskQueue: taskQueue)
OptimizelyFlutterLogger.setChannel(loggerChannel)
}


public func detachFromEngine(for registrar: FlutterPluginRegistrar) {
Self.channel?.setMethodCallHandler(nil)
Self.channel = nil
OptimizelyFlutterLogger.clearChannel()
}

/// Part of FlutterPlugin protocol to handle communication with flutter sdk.
/// All method handlers receive a main-thread-safe result callback so that
/// any handler calling result() from a background thread (e.g. async SDK
Expand Down
Loading