Skip to content

Commit 8794711

Browse files
committed
Add structured logging as an alternative to stdout printing
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).
1 parent ab7455c commit 8794711

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/TelemetryClient/LogHandler.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import OSLog
23

34
public struct LogHandler {
45
public enum LogLevel: Int, CustomStringConvertible {
@@ -16,6 +17,17 @@ public struct LogHandler {
1617
return "ERROR"
1718
}
1819
}
20+
21+
public var osLogLevel: OSLogType {
22+
switch self {
23+
case .debug:
24+
return OSLogType.debug
25+
case .info:
26+
return OSLogType.info
27+
case .error:
28+
return OSLogType.error
29+
}
30+
}
1931
}
2032

2133
let logLevel: LogLevel
@@ -27,6 +39,16 @@ public struct LogHandler {
2739
}
2840
}
2941

42+
@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *)
43+
public static var oslog = { logLevel in
44+
LogHandler(logLevel: logLevel) { level, message in
45+
Logger(
46+
subsystem: "TelemetryDeck",
47+
category: "LogHandler"
48+
).log(level: logLevel.osLogLevel, "\(message, privacy: .public)")
49+
}
50+
}
51+
3052
public static var stdout = { logLevel in
3153
LogHandler(logLevel: logLevel) { level, message in
3254
print("[TelemetryDeck: \(level.description)] \(message)")

0 commit comments

Comments
 (0)