From acb4dc23d53f2d85d906ac51cb9591145c10d485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihat=20Gu=CC=88ndu=CC=88z?= Date: Thu, 31 Oct 2024 16:32:58 +0100 Subject: [PATCH] Add support for OSLog and default to it on modern systems --- .../TelemetryDeck/Helpers/LogHandler.swift | 30 ++++++++++++++++++- Sources/TelemetryDeck/TelemetryClient.swift | 4 +-- .../TelemetryDeckTests/LogHandlerTests.swift | 2 +- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Sources/TelemetryDeck/Helpers/LogHandler.swift b/Sources/TelemetryDeck/Helpers/LogHandler.swift index 73695bd..ffb3efe 100644 --- a/Sources/TelemetryDeck/Helpers/LogHandler.swift +++ b/Sources/TelemetryDeck/Helpers/LogHandler.swift @@ -1,4 +1,7 @@ import Foundation +#if canImport(OSLog) +import OSLog +#endif public struct LogHandler: Sendable { public enum LogLevel: Int, CustomStringConvertible, Sendable { @@ -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)") } diff --git a/Sources/TelemetryDeck/TelemetryClient.swift b/Sources/TelemetryDeck/TelemetryClient.swift index 89a7fe1..af4eb9e 100644 --- a/Sources/TelemetryDeck/TelemetryClient.swift +++ b/Sources/TelemetryDeck/TelemetryClient.swift @@ -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. /// diff --git a/Tests/TelemetryDeckTests/LogHandlerTests.swift b/Tests/TelemetryDeckTests/LogHandlerTests.swift index 15e21c4..58fdf72 100644 --- a/Tests/TelemetryDeckTests/LogHandlerTests.swift +++ b/Tests/TelemetryDeckTests/LogHandlerTests.swift @@ -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() {