Skip to content

Commit f46efa3

Browse files
committed
add autoclosure to log message
1 parent b7a95a5 commit f46efa3

File tree

9 files changed

+89
-78
lines changed

9 files changed

+89
-78
lines changed

DemoSwiftApp/AppDelegate.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Amplitude_iOS
2323

2424
@UIApplicationMain
2525
class AppDelegate: UIResponder, UIApplicationDelegate {
26+
let logLevel = OptimizelyLogLevel.debug
27+
2628
let sdkKey = "FCnSegiEkRry9rhVMroit4"
2729
let datafileName = "demoTestDatafile"
2830
let experimentKey = "background_experiment"
@@ -51,13 +53,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5153
// - initialize immediately with the given JSON datafile or its cached copy
5254
// - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings
5355

54-
initializeOptimizelySDKWithCustomization()
56+
initializeOptimizelySDKAsynchronous()
5557
}
5658

5759
// MARK: - Initialization Examples
5860

5961
func initializeOptimizelySDKAsynchronous() {
60-
optimizely = OptimizelyClient(sdkKey: sdkKey)
62+
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
6163

6264
optimizely.start { result in
6365
switch result {
@@ -79,7 +81,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7981
fatalError("Local datafile cannot be found")
8082
}
8183

82-
optimizely = OptimizelyClient(sdkKey: sdkKey)
84+
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
8385

8486
do {
8587
let datafileJSON = try String(contentsOfFile: localDatafilePath, encoding: .utf8)
@@ -103,7 +105,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
103105

104106
optimizely = OptimizelyClient(sdkKey: sdkKey,
105107
logger: customLogger,
106-
periodicDownloadInterval: customDownloadIntervalInSecs)
108+
periodicDownloadInterval: customDownloadIntervalInSecs,
109+
defaultLogLevel: logLevel)
107110

108111
// notification listeners
109112

DemoSwiftApp/Customization/CustomLogger.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class CustomLogger: OPTLogger {
1515
required init() {
1616
}
1717

18-
public func log(level: OptimizelyLogLevel, message: String) {
18+
public func log(level: OptimizelyLogLevel, message: @autoclosure () -> String) {
1919
if level.rawValue <= CustomLogger.logLevel.rawValue {
20-
print("🐱 - [\(level.name)] Kitty - \(message)")
20+
print("🐱 - [\(level.name)] Kitty - \(message())")
2121
}
2222
}
2323
}

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
148148
// we succeeded. remove the batch size sent.
149149
if let removedItem:[EventForDispatch] = self.dataStore.removeFirstItems(count: self.batchSize) {
150150
if self.batchSize == 1 && removedItem.first != event {
151-
self.logger?.log(level: .error, message: "Removed event different from sent event")
151+
self.logger?.e("Removed event different from sent event")
152152
}
153153
else {
154-
self.logger?.log(level: .debug, message: "Successfully sent event " + event.body.debugDescription)
154+
self.logger?.d("Successfully sent event: \(event)")
155155
}
156156
}
157157
else {
158-
self.logger?.log(level: .error, message: "Removed event nil for sent item")
158+
self.logger?.e("Removed event nil for sent item")
159159
}
160160
// reset failureCount
161161
failureCount = 0
@@ -172,7 +172,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
172172
}
173173
}
174174
// our send is done.
175-
defer { self.notify.leave() }
175+
self.notify.leave()
176176

177177
}
178178
// wait for send

OptimizelySDK/Customization/DefaultLogger.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ open class DefaultLogger : OPTLogger {
3333
required public init() {
3434
}
3535

36-
open func log(level: OptimizelyLogLevel, message: String) {
36+
open func log(level: OptimizelyLogLevel, message: @autoclosure () -> String) {
3737
if level.rawValue > DefaultLogger.logLevel.rawValue {
3838
return
3939
}
40-
let message = "[OPTIMIZELY][" + level.name + "]:" + message
40+
let message = "[OPTIMIZELY][" + level.name + "]:" + message()
4141
NSLog(message)
4242
}
4343
}

OptimizelySDK/Customization/Protocols/OPTLogger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation
3535
- Parameter level: The priority level of the log.
3636
- Parameter message: The message to log.
3737
*/
38-
func log(level: OptimizelyLogLevel, message: String)
38+
func log(level: OptimizelyLogLevel, message: @autoclosure () -> String)
3939

4040
}
4141

OptimizelySDK/Data Model/DispatchEvents/EventForDispatch.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ import Foundation
3333
return url == object.url && body == object.body
3434
}
3535
}
36+
37+
extension EventForDispatch {
38+
override public var description: String {
39+
return "[url] \(url) (" + (String(data: body, encoding: .utf8) ?? "UNKNOWN") + ")"
40+
}
41+
}

OptimizelySDK/Data Model/ProjectConfig.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,12 @@ extension ProjectConfig {
277277
variationKey = variationKey.trimmingCharacters(in: NSCharacterSet.whitespaces)
278278

279279
guard !variationKey.isEmpty else {
280+
logger?.e(.variationKeyInvalid(experimentKey, variationKey))
280281
return false
281282
}
282283

283284
guard let variation = experiment.variations.filter({$0.key == variationKey }).first else {
285+
logger?.e(.variationKeyInvalid(experimentKey, variationKey))
284286
return false
285287
}
286288

OptimizelySDK/Optimizely/OptimizelyError.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,26 @@ extension OptimizelyError: CustomStringConvertible {
9090
switch self {
9191
case .generic: message = "Unknown reason"
9292
case .sdkNotReady: message = "Optimizely SDK not configured properly yet"
93-
case .experimentKeyInvalid(let key): message = "Experiment key \(key) is not in datafile. It is either invalid, paused, or archived."
94-
case .experimentIdInvalid(let id): message = "Experiment ID \(id) is not in datafile."
93+
case .experimentKeyInvalid(let key): message = "Experiment key (\(key)) is not in datafile. It is either invalid, paused, or archived."
94+
case .experimentIdInvalid(let id): message = "Experiment ID (\(id)) is not in datafile."
9595
case .experimentHasNoTrafficAllocation(let key): message = "No traffic allocation rules are defined for experiement (\(key))."
96-
case .featureKeyInvalid(let key): message = "Feature key \(key) is not in datafile."
97-
case .variationKeyInvalid(let expKey, let varKey): message = "No variation key \(varKey) defined in datafile for experiment \(expKey)."
98-
case .variationIdInvalid(let expKey, let varId): message = "No variation ID \(varId) defined in datafile for experiment \(expKey)."
99-
case .variationUnknown(let userId, let key): message = "User \(userId) does not meet conditions to be in experiment/feature \(key)."
100-
case .variableKeyInvalid(let varKey, let feature): message = "Variable with key \(varKey) associated with feature with key \(feature) is not in datafile."
101-
case .variableValueInvalid(let key): message = "Variable value for key \(key) is invalid or wrong type"
102-
case .eventKeyInvalid(let key): message = "Event key \(key) is not in datafile."
103-
case .eventBuildFailure(let key): message = "Failed to create a dispatch event \(key)"
96+
case .featureKeyInvalid(let key): message = "Feature key (\(key)) is not in datafile."
97+
case .variationKeyInvalid(let expKey, let varKey): message = "No variation key (\(varKey)) defined in datafile for experiment (\(expKey))."
98+
case .variationIdInvalid(let expKey, let varId): message = "No variation ID (\(varId)) defined in datafile for experiment (\(expKey))."
99+
case .variationUnknown(let userId, let key): message = "User (\(userId)) does not meet conditions to be in experiment/feature (\(key))."
100+
case .variableKeyInvalid(let varKey, let feature): message = "Variable with key (\(varKey)) associated with feature with key (\(feature)) is not in datafile."
101+
case .variableValueInvalid(let key): message = "Variable value for key (\(key)) is invalid or wrong type"
102+
case .eventKeyInvalid(let key): message = "Event key (\(key)) is not in datafile."
103+
case .eventBuildFailure(let key): message = "Failed to create a dispatch event (\(key))"
104104
case .eventTagsFormatInvalid: message = "Provided event tags are in an invalid format."
105-
case .attributesKeyInvalid(let key): message = "Attribute key \(key) is not in datafile."
106-
case .attributeValueInvalid(let key): message = "Attribute value for \(key) is invalid."
105+
case .attributesKeyInvalid(let key): message = "Attribute key (\(key)) is not in datafile."
106+
case .attributeValueInvalid(let key): message = "Attribute value for (\(key)) is invalid."
107107
case .attributeFormatInvalid: message = "Provided attributes are in an invalid format."
108-
case .groupIdInvalid(let id): message = "Group ID \(id) is not in datafile."
108+
case .groupIdInvalid(let id): message = "Group ID (\(id)) is not in datafile."
109109
case .groupHasNoTrafficAllocation(let id): message = "No traffic allocation rules are defined for group (\(id))"
110-
case .rolloutIdInvalid(let id, let feature): message = "Invalid rollout ID \(id) attached to feature \(feature)"
110+
case .rolloutIdInvalid(let id, let feature): message = "Invalid rollout ID (\(id)) attached to feature (\(feature))"
111111

112-
case .conditionNoMatchingAudience(let id): message = "Audience \(id) is not in datafile."
112+
case .conditionNoMatchingAudience(let id): message = "Audience (\(id)) is not in datafile."
113113
case .conditionInvalidFormat(let hint): message = "Condition has an invalid format (\(hint))"
114114
case .conditionCannotBeEvaluated(let hint): message = "Condition cannot be evaluated (\(hint))"
115115
case .evaluateAttributeInvalidType(let hint): message = "Evaluation attribute has an invalid value (\(hint))"
@@ -120,12 +120,12 @@ extension OptimizelyError: CustomStringConvertible {
120120
case .userAttributeInvalidFormat(let hint): message = "UserAttribute has an invalid format (\(hint))"
121121

122122
case .userIdInvalid: message = "Provided user ID is in an invalid format."
123-
case .bucketingIdInvalid (let id): message = "Invalid bucketing ID: \(id)"
123+
case .bucketingIdInvalid (let id): message = "Invalid bucketing ID (\(id))"
124124
case .userProfileInvalid: message = "Provided user profile object is invalid."
125125

126126
case .datafileDownloadFailed(let hint): message = "Datafile download failed (\(hint))"
127127
case .dataFileInvalid: message = "Provided datafile is in an invalid format."
128-
case .dataFileVersionInvalid (let version): message = "Provided datafile version \(version) is not supported."
128+
case .dataFileVersionInvalid (let version): message = "Provided datafile version (\(version)) is not supported."
129129
case .datafileSavingFailed(let hint): message = "Datafile save failed (\(hint))"
130130
case .datafileLoadingFailed(let hint): message = "Datafile load failed (\(hint))"
131131

0 commit comments

Comments
 (0)