Skip to content

Commit

Permalink
Add structured logging as an alternative to stdout printing
Browse files Browse the repository at this point in the history
This fixes #131 and adds a `oslog` handler for logging as an option.

Rather than change all logging to use this new endpoint by default I
left it as an option for the client to decide as perhaps people could be
relying on stdout printing.

The new variable is gated around the platforms which support this new
structured logging API (iOS 14 and similar).
  • Loading branch information
davidrothera committed Apr 22, 2024
1 parent ab7455c commit 8794711
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/TelemetryClient/LogHandler.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import OSLog

public struct LogHandler {
public enum LogLevel: Int, CustomStringConvertible {
Expand All @@ -16,6 +17,17 @@ public struct LogHandler {
return "ERROR"
}
}

public var osLogLevel: OSLogType {
switch self {
case .debug:

Check warning on line 23 in Sources/TelemetryClient/LogHandler.swift

View workflow job for this annotation

GitHub Actions / Lint Code

Lines should not have trailing whitespace (trailing_whitespace)
return OSLogType.debug
case .info:
return OSLogType.info
case .error:
return OSLogType.error
}
}
}

let logLevel: LogLevel
Expand All @@ -27,6 +39,16 @@ public struct LogHandler {
}
}

@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
public static var oslog = { logLevel in
LogHandler(logLevel: logLevel) { level, message in

Check warning on line 44 in Sources/TelemetryClient/LogHandler.swift

View workflow job for this annotation

GitHub Actions / Lint Code

Unused parameter in a closure should be replaced with _ (unused_closure_parameter)
Logger(
subsystem: "TelemetryDeck",
category: "LogHandler"
).log(level: logLevel.osLogLevel, "\(message, privacy: .public)")
}
}

public static var stdout = { logLevel in
LogHandler(logLevel: logLevel) { level, message in
print("[TelemetryDeck: \(level.description)] \(message)")
Expand Down

0 comments on commit 8794711

Please sign in to comment.