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 structured logging as an alternative to stdout printing #150

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we really import OSLog just like that? I believe it's only available starting with iOS 15, so we need to at least check for canImport here, I think, since we're supporting iOS 12.


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

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

Check failure 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)

Check failure 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 @@ -32,6 +44,16 @@
}
}

@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 failure on line 49 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)

Check failure on line 49 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)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the specifier privacy: .public is redundant here since during logging the privacy level is public by default.

}
}

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