|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Logging API open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018-2025 Apple Inc. and the Swift Logging API project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift Logging API project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if canImport(os) |
| 16 | +public import Logging |
| 17 | +import LoggingAttributes |
| 18 | +import os |
| 19 | + |
| 20 | +/// A redaction-aware log handler that uses Apple's unified logging system (os.Logger). |
| 21 | +/// |
| 22 | +/// This handler leverages OSLog's native privacy support to automatically redact |
| 23 | +/// metadata values marked with `.sensitive` when viewing logs outside of |
| 24 | +/// development environments. |
| 25 | +/// |
| 26 | +/// ## Features |
| 27 | +/// |
| 28 | +/// - **Native Privacy Support**: Uses OSLog's `privacy: .private` and `privacy: .public` annotations |
| 29 | +/// - **System Integration**: Logs appear in Console.app and can be viewed with `log` command |
| 30 | +/// - **Performance**: Zero-cost when logging is disabled at the system level |
| 31 | +/// - **Subsystem Organization**: Groups logs by subsystem and category for better filtering |
| 32 | +/// |
| 33 | +/// ## Usage |
| 34 | +/// |
| 35 | +/// ```swift |
| 36 | +/// let handler = OSLogHandler(subsystem: "com.example.myapp", category: "network") |
| 37 | +/// let logger = Logger(label: "network") { _ in handler } |
| 38 | +/// |
| 39 | +/// let userId = "12345" |
| 40 | +/// logger.info("User logged in", metadata: [ |
| 41 | +/// "user.id": "\(userId, sensitivity: .sensitive)", |
| 42 | +/// "action": "\(\"login\", sensitivity: .public)" |
| 43 | +/// ]) |
| 44 | +/// ``` |
| 45 | +/// |
| 46 | +/// ## Redaction Behavior |
| 47 | +/// |
| 48 | +/// - `.sensitive` metadata -> OSLog `privacy: .private` (redacted as `<private>` in logs) |
| 49 | +/// - `.public` metadata -> OSLog `privacy: .public` (always visible) |
| 50 | +/// - Plain metadata -> Treated as `.public` by default |
| 51 | +@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) |
| 52 | +public struct OSLogHandler: LogHandler { |
| 53 | + private var osLogger: os.Logger |
| 54 | + |
| 55 | + public var metadata: Logging.Logger.Metadata = [:] |
| 56 | + public var metadataProvider: Logging.Logger.MetadataProvider? |
| 57 | + public var logLevel: Logging.Logger.Level = .info |
| 58 | + |
| 59 | + /// Controls whether to append a suffix listing redacted keys. |
| 60 | + public var showRedactedKeysList: Bool = true |
| 61 | + |
| 62 | + /// Creates an OSLog handler with the specified subsystem and category. |
| 63 | + public init(subsystem: String, category: String) { |
| 64 | + self.osLogger = os.Logger(subsystem: subsystem, category: category) |
| 65 | + } |
| 66 | + |
| 67 | + public func log(event: LogEvent) { |
| 68 | + var merged = self.metadata |
| 69 | + |
| 70 | + if let provider = self.metadataProvider { |
| 71 | + let provided = provider.get() |
| 72 | + merged.merge(provided, uniquingKeysWith: { _, rhs in rhs }) |
| 73 | + } |
| 74 | + |
| 75 | + if let eventMetadata = event.metadata { |
| 76 | + merged.merge(eventMetadata, uniquingKeysWith: { _, rhs in rhs }) |
| 77 | + } |
| 78 | + |
| 79 | + if let error = event.error { |
| 80 | + if merged["error.message"] == nil { |
| 81 | + merged["error.message"] = "\(error)" |
| 82 | + } |
| 83 | + if merged["error.type"] == nil { |
| 84 | + merged["error.type"] = "\(String(reflecting: type(of: error)))" |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if merged.isEmpty { |
| 89 | + self.osLogger.log(level: self.mapLogLevel(event.level), "\(event.message.description)") |
| 90 | + } else if merged.contains(where: { $0.value.attributes.sensitivity == .sensitive }) { |
| 91 | + self.logToOSLogWithRedaction(level: event.level, message: event.message, metadata: merged) |
| 92 | + } else { |
| 93 | + let metadataString = merged.sorted(by: { $0.key < $1.key }) |
| 94 | + .map { "\($0.key)=\($0.value)" } |
| 95 | + .joined(separator: " ") |
| 96 | + self.osLogger.log( |
| 97 | + level: self.mapLogLevel(event.level), |
| 98 | + "\(event.message.description) \(metadataString, privacy: .public)" |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public subscript(metadataKey key: String) -> Logging.Logger.Metadata.Value? { |
| 104 | + get { self.metadata[key] } |
| 105 | + set { self.metadata[key] = newValue } |
| 106 | + } |
| 107 | + |
| 108 | + // MARK: - Private Helpers |
| 109 | + |
| 110 | + private func logToOSLogWithRedaction( |
| 111 | + level: Logging.Logger.Level, |
| 112 | + message: Logging.Logger.Message, |
| 113 | + metadata: Logging.Logger.Metadata |
| 114 | + ) { |
| 115 | + let osLogType = self.mapLogLevel(level) |
| 116 | + |
| 117 | + let publicMetadata = metadata.filter { $0.value.attributes.sensitivity != .sensitive } |
| 118 | + let redactedMetadata = metadata.filter { $0.value.attributes.sensitivity == .sensitive } |
| 119 | + |
| 120 | + let redactedKeysSuffix = self.showRedactedKeysList ? self.formatRedactedKeysSuffix(redactedMetadata) : "" |
| 121 | + |
| 122 | + let publicString = self.formatMetadataValues(publicMetadata) |
| 123 | + let redactedString = self.formatMetadataValues(redactedMetadata) |
| 124 | + |
| 125 | + switch (!publicString.isEmpty, !redactedString.isEmpty) { |
| 126 | + case (true, true): |
| 127 | + self.osLogger.log( |
| 128 | + level: osLogType, |
| 129 | + "\(message.description) \(publicString, privacy: .public) \(redactedString, privacy: .private)\(redactedKeysSuffix, privacy: .public)" |
| 130 | + ) |
| 131 | + case (true, false): |
| 132 | + self.osLogger.log(level: osLogType, "\(message.description) \(publicString, privacy: .public)") |
| 133 | + case (false, true): |
| 134 | + self.osLogger.log( |
| 135 | + level: osLogType, |
| 136 | + "\(message.description) \(redactedString, privacy: .private)\(redactedKeysSuffix, privacy: .public)" |
| 137 | + ) |
| 138 | + case (false, false): |
| 139 | + self.osLogger.log(level: osLogType, "\(message.description)") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private func mapLogLevel(_ level: Logging.Logger.Level) -> OSLogType { |
| 144 | + switch level { |
| 145 | + case .trace: return .debug |
| 146 | + case .debug: return .debug |
| 147 | + case .info: return .info |
| 148 | + case .notice: return .default |
| 149 | + case .warning: return .error |
| 150 | + case .error: return .error |
| 151 | + case .critical: return .fault |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private func formatMetadataValues(_ metadata: Logging.Logger.Metadata) -> String { |
| 156 | + metadata |
| 157 | + .sorted(by: { $0.key < $1.key }) |
| 158 | + .map { "\($0.key)=\($0.value)" } |
| 159 | + .joined(separator: " ") |
| 160 | + } |
| 161 | + |
| 162 | + private func formatRedactedKeysSuffix(_ metadata: Logging.Logger.Metadata) -> String { |
| 163 | + if metadata.isEmpty { return "" } |
| 164 | + let keys = metadata.keys.sorted().joined(separator: ", ") |
| 165 | + let keyWord = metadata.count == 1 ? "key" : "keys" |
| 166 | + let isWord = metadata.count == 1 ? "is" : "are" |
| 167 | + return " (\(keyWord) \(keys) \(isWord) marked private)" |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#endif |
0 commit comments