-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support customisable LogHandler (#92)
* Support customisable LogHandler By default, this will increase slightly the amount of logging that end clients see - however it is now far more configurable. `showDebugLogs` has been deprecated with users being encouraged to use `logHandler`. The debug logs parameter is no longer used. * Update docs
- Loading branch information
Showing
7 changed files
with
109 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Foundation | ||
|
||
public struct LogHandler { | ||
public enum LogLevel: Int, CustomStringConvertible { | ||
case debug = 0 | ||
case info = 1 | ||
case error = 2 | ||
|
||
public var description: String { | ||
switch self { | ||
case .debug: | ||
return "DEBUG" | ||
case .info: | ||
return "INFO" | ||
case .error: | ||
return "ERROR" | ||
} | ||
} | ||
} | ||
|
||
let logLevel: LogLevel | ||
let handler: (LogLevel, String) -> Void | ||
|
||
internal func log(_ level: LogLevel = .info, message: String) { | ||
if level.rawValue >= logLevel.rawValue { | ||
handler(level, message) | ||
} | ||
} | ||
|
||
public static var stdout = { logLevel in | ||
LogHandler(logLevel: logLevel) { level, message in | ||
print("[TelemetryDeck: \(level.description)] \(message)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@testable import TelemetryClient | ||
import XCTest | ||
|
||
final class LogHandlerTests: XCTestCase { | ||
func testLogHandler_stdoutLogLevelDefined() { | ||
XCTAssertEqual(LogHandler.stdout(.error).logLevel, .error) | ||
} | ||
|
||
func testLogHandler_logLevelRespected() { | ||
var counter = 0 | ||
|
||
let handler = LogHandler(logLevel: .info) { _, _ in | ||
counter += 1 | ||
} | ||
|
||
XCTAssertEqual(counter, 0) | ||
handler.log(.debug, message: "") | ||
XCTAssertEqual(counter, 0) | ||
handler.log(.info, message: "") | ||
XCTAssertEqual(counter, 1) | ||
handler.log(.error, message: "") | ||
XCTAssertEqual(counter, 2) | ||
} | ||
|
||
func testLogHandler_defaultLogLevel() { | ||
var lastLevel: LogHandler.LogLevel? | ||
|
||
let handler = LogHandler(logLevel: .debug) { level, _ in | ||
lastLevel = level | ||
} | ||
|
||
handler.log(message: "") | ||
XCTAssertEqual(lastLevel, .info) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters