Skip to content

Commit 0cd36fa

Browse files
authored
Fixed log toggling (#86)
1 parent ce47099 commit 0cd36fa

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

Sources/Segment/Analytics.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public class Analytics {
1616
internal var storage: Storage
1717

1818
/// Enabled/disables debug logging to trace your data going through the SDK.
19-
public var debugLogsEnabled = false
19+
public static var debugLogsEnabled = false {
20+
didSet {
21+
SegmentLog.loggingEnabled = debugLogsEnabled
22+
}
23+
}
2024

2125
public var timeline: Timeline
2226

Sources/Segment/Plugins/Logger/SegmentLog.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ internal class SegmentLog: UtilityPlugin {
1616
let type = PluginType.utility
1717

1818
fileprivate var loggingMediator = [LoggingType: LogTarget]()
19-
internal static var loggingEnabled = true
19+
20+
// Default to no, enable to see local logs
21+
internal static var loggingEnabled = false
2022

2123
// For internal use only. Note: This will contain the last created instance
2224
// of analytics when used in a multi-analytics environment.

Tests/Segment-Tests/LogTarget_Tests.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ final class LogTarget_Tests: XCTestCase {
3232
override func setUp() {
3333
analytics = Analytics(configuration: Configuration(writeKey: "test"))
3434
analytics?.add(plugin: mockLogger)
35+
36+
// Enable logging for all tests
37+
SegmentLog.loggingEnabled = true
3538
}
3639

3740
override func tearDown() {
3841
analytics = nil
39-
SegmentLog.loggingEnabled = true
42+
43+
// Reset to default state the system should be in from start
44+
SegmentLog.loggingEnabled = false
4045
}
4146

4247
func testMetric() {
@@ -142,6 +147,20 @@ final class LogTarget_Tests: XCTestCase {
142147
analytics?.history(event: TrackEvent(event: "Tester", properties: nil), sender: self)
143148
}
144149

145-
150+
func testLoggingDisabledByDefault() {
151+
SegmentLog.loggingEnabled = false
152+
XCTAssertFalse(SegmentLog.loggingEnabled, "Logging should not default to enabled")
153+
}
154+
155+
func testLoggingEnabledFromAnalytics() {
156+
SegmentLog.loggingEnabled = false
157+
158+
Analytics.debugLogsEnabled = true
159+
XCTAssertTrue(SegmentLog.loggingEnabled, "Logging should change to enabled")
160+
161+
Analytics.debugLogsEnabled = false
162+
XCTAssertFalse(SegmentLog.loggingEnabled, "Logging should reset to disabled")
163+
}
164+
146165
}
147166

0 commit comments

Comments
 (0)