Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OSLog and default to it on modern systems #195

Merged
merged 1 commit into from
Nov 4, 2024
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
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