diff --git a/Sources/Segment/Analytics.swift b/Sources/Segment/Analytics.swift index 89cf4a2d..d4bd33cd 100644 --- a/Sources/Segment/Analytics.swift +++ b/Sources/Segment/Analytics.swift @@ -16,7 +16,11 @@ public class Analytics { internal var storage: Storage /// Enabled/disables debug logging to trace your data going through the SDK. - public var debugLogsEnabled = false + public static var debugLogsEnabled = false { + didSet { + SegmentLog.loggingEnabled = debugLogsEnabled + } + } public var timeline: Timeline diff --git a/Sources/Segment/Plugins/Logger/SegmentLog.swift b/Sources/Segment/Plugins/Logger/SegmentLog.swift index ce079faf..d75fc4ac 100644 --- a/Sources/Segment/Plugins/Logger/SegmentLog.swift +++ b/Sources/Segment/Plugins/Logger/SegmentLog.swift @@ -16,7 +16,9 @@ internal class SegmentLog: UtilityPlugin { let type = PluginType.utility fileprivate var loggingMediator = [LoggingType: LogTarget]() - internal static var loggingEnabled = true + + // Default to no, enable to see local logs + internal static var loggingEnabled = false // For internal use only. Note: This will contain the last created instance // of analytics when used in a multi-analytics environment. diff --git a/Tests/Segment-Tests/LogTarget_Tests.swift b/Tests/Segment-Tests/LogTarget_Tests.swift index ab90c5a7..75944c79 100644 --- a/Tests/Segment-Tests/LogTarget_Tests.swift +++ b/Tests/Segment-Tests/LogTarget_Tests.swift @@ -32,11 +32,16 @@ final class LogTarget_Tests: XCTestCase { override func setUp() { analytics = Analytics(configuration: Configuration(writeKey: "test")) analytics?.add(plugin: mockLogger) + + // Enable logging for all tests + SegmentLog.loggingEnabled = true } override func tearDown() { analytics = nil - SegmentLog.loggingEnabled = true + + // Reset to default state the system should be in from start + SegmentLog.loggingEnabled = false } func testMetric() { @@ -142,6 +147,20 @@ final class LogTarget_Tests: XCTestCase { analytics?.history(event: TrackEvent(event: "Tester", properties: nil), sender: self) } - + func testLoggingDisabledByDefault() { + SegmentLog.loggingEnabled = false + XCTAssertFalse(SegmentLog.loggingEnabled, "Logging should not default to enabled") + } + + func testLoggingEnabledFromAnalytics() { + SegmentLog.loggingEnabled = false + + Analytics.debugLogsEnabled = true + XCTAssertTrue(SegmentLog.loggingEnabled, "Logging should change to enabled") + + Analytics.debugLogsEnabled = false + XCTAssertFalse(SegmentLog.loggingEnabled, "Logging should reset to disabled") + } + }