Skip to content

Commit

Permalink
Merge pull request #195 from TelemetryDeck/feature/oslog
Browse files Browse the repository at this point in the history
Add support for OSLog and default to it on modern systems
  • Loading branch information
Jeehut authored Nov 4, 2024
2 parents 8deeddc + acb4dc2 commit eb38add
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
30 changes: 29 additions & 1 deletion Sources/TelemetryDeck/Helpers/LogHandler.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
#if canImport(OSLog)
import OSLog
#endif

public struct LogHandler: Sendable {
public enum LogLevel: Int, CustomStringConvertible, Sendable {
Expand Down Expand Up @@ -32,7 +35,32 @@ public struct LogHandler: Sendable {
}
}

public static func stdout(_ logLevel: LogLevel) -> LogHandler {
public static func standard(_ logLevel: LogLevel) -> LogHandler {
#if canImport(OSLog)
if #available(iOS 15, macOS 11, tvOS 15, watchOS 8, *) {
return Self.oslog(logLevel)
} else {
return Self.stdout(logLevel)
}
#else
return Self.stdout(logLevel)
#endif
}

@available(iOS 15, macOS 11, tvOS 15, watchOS 8, *)
private static func oslog(_ logLevel: LogLevel) -> LogHandler {
LogHandler(logLevel: logLevel) { level, message in
let logger = Logger(subsystem: "TelemetryDeck", category: "LogHandler")

switch level {
case .debug: logger.debug("\(message)")
case .info: logger.info("\(message)")
case .error: logger.error("\(message)")
}
}
}

private static func stdout(_ logLevel: LogLevel) -> LogHandler {
LogHandler(logLevel: logLevel) { level, message in
print("[TelemetryDeck: \(level.description)] \(message)")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TelemetryDeck/TelemetryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public struct TelemetryManagerConfiguration: Sendable {

/// A strategy for handling logs.
///
/// Defaults to `print` with info/errror messages - debug messages are not outputted. Set to `nil` to disable all logging from TelemetryDeck SDK.
/// Defaults to `OSLog.Logger` with info/errror messages - debug messages are not outputted. Set to `nil` to disable all logging from TelemetryDeck SDK.
///
/// - NOTE: If ``swiftUIPreviewMode`` is `true` (by default only when running SwiftUI previews), this value is effectively ignored, working like it's set to `nil`.
public var logHandler: LogHandler? = LogHandler.stdout(.info)
public var logHandler: LogHandler? = LogHandler.standard(.info)

/// An array of signal metadata enrichers: a system for adding dynamic metadata to signals as they are recorded.
///
Expand Down
2 changes: 1 addition & 1 deletion Tests/TelemetryDeckTests/LogHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class LogHandlerTests: XCTestCase {
var lastLevel: LogHandler.LogLevel?

func testLogHandler_stdoutLogLevelDefined() {
XCTAssertEqual(LogHandler.stdout(.error).logLevel, .error)
XCTAssertEqual(LogHandler.standard(.error).logLevel, .error)
}

func testLogHandler_logLevelRespected() {
Expand Down

0 comments on commit eb38add

Please sign in to comment.