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
6 changes: 5 additions & 1 deletion Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion Sources/Segment/Plugins/Logger/SegmentLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 21 additions & 2 deletions Tests/Segment-Tests/LogTarget_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
}

}