-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOptimizelyFlutterLogger.swift
More file actions
45 lines (36 loc) · 1.33 KB
/
OptimizelyFlutterLogger.swift
File metadata and controls
45 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Flutter
import Optimizely
public class OptimizelyFlutterLogger: NSObject, OPTLogger {
static var LOGGER_CHANNEL: String = "optimizely_flutter_sdk_logger";
public static var logLevel: OptimizelyLogLevel = .info
private static var loggerChannel: FlutterMethodChannel?
public required override init() {
super.init()
}
public static func setChannel(_ channel: FlutterMethodChannel) {
if loggerChannel == nil {
loggerChannel = channel
}
}
public static func clearChannel() {
loggerChannel = nil
}
public func log(level: OptimizelyLogLevel, message: String) {
// Early return if level check fails
guard level.rawValue <= OptimizelyFlutterLogger.logLevel.rawValue else {
return
}
// Ensure we have a valid channel
guard let channel = Self.loggerChannel else {
print("[OptimizelyFlutterLogger] ERROR: No logger channel available!")
return
}
// https://docs.flutter.dev/platform-integration/platform-channels#jumping-to-the-main-thread-in-ios
DispatchQueue.main.async {
channel.invokeMethod("log", arguments: [
"level": level.rawValue,
"message": message
])
}
}
}