|
| 1 | +import Foundation |
| 2 | +import os.log |
| 3 | + |
| 4 | +// MARK: - Unified Logging System |
| 5 | + |
| 6 | +/// Represents different categories of logs within the SDK. |
| 7 | +enum LogCategory: String { |
| 8 | + /// networkTracing category is used for the network request and response traces |
| 9 | + case networkTracing = "NetworkTracing" |
| 10 | + /// configuration category is used for SDK configuration related logs |
| 11 | + case configuration = "Configuration" |
| 12 | +} |
| 13 | + |
| 14 | +/// Log levels supported by the unified logging system. |
| 15 | +enum LogLevel { |
| 16 | + /// Detailed information for debugging |
| 17 | + case debug |
| 18 | + /// General informational messages |
| 19 | + case info |
| 20 | + /// Warning messages for potentially problematic situations |
| 21 | + case warning |
| 22 | + /// Error messages for failures |
| 23 | + case error |
| 24 | + /// Critical system errors |
| 25 | + case fault |
| 26 | +} |
| 27 | + |
| 28 | +/// Unified logging interface for the SDK. |
| 29 | +/// Provides a consistent, type-safe API for logging across all SDK components. |
| 30 | +enum Auth0Log { |
| 31 | + |
| 32 | + /// Shared logging service instance (can be replaced for testing). |
| 33 | + static var loggingService: UnifiedLogging = OSUnifiedLoggingService() |
| 34 | + |
| 35 | + /// Centralized subsystem identifier for all Auth0 logs. |
| 36 | + static var subsystem: String { OSUnifiedLoggingService.subsystem } |
| 37 | + |
| 38 | + /// Log a message with the specified category and level. |
| 39 | + /// |
| 40 | + /// Use this method for fine-grained control over logging. For common use cases, |
| 41 | + /// prefer the convenience methods: `debug()`, `info()`, `warning()`, `error()`, `fault()`. |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - category: The log category for filtering and organization. See `LogCategory` for available categories. |
| 45 | + /// - level: The severity level of the log. Defaults to `.debug`. See `LogLevel` for available levels. |
| 46 | + /// - message: The message to log. Evaluated lazily via autoclosure for performance. |
| 47 | + static func log( |
| 48 | + _ category: LogCategory, |
| 49 | + level: LogLevel = .debug, |
| 50 | + message: String |
| 51 | + ) { |
| 52 | + loggingService.log(category, level: level, message: message) |
| 53 | + } |
| 54 | + |
| 55 | + /// Log a debug message. |
| 56 | + /// |
| 57 | + /// Use for detailed information useful during development and debugging. |
| 58 | + /// |
| 59 | + /// - Parameters: |
| 60 | + /// - category: The log category for filtering |
| 61 | + /// - message: The message to log |
| 62 | + static func debug( |
| 63 | + _ category: LogCategory, |
| 64 | + _ message: String |
| 65 | + ) { |
| 66 | + log(category, level: .debug, message: message) |
| 67 | + } |
| 68 | + |
| 69 | + /// Log an informational message. |
| 70 | + /// |
| 71 | + /// Use for general informational messages about application state. |
| 72 | + /// |
| 73 | + /// - Parameters: |
| 74 | + /// - category: The log category for filtering |
| 75 | + /// - message: The message to log |
| 76 | + static func info( |
| 77 | + _ category: LogCategory, |
| 78 | + _ message: String |
| 79 | + ) { |
| 80 | + log(category, level: .info, message: message) |
| 81 | + } |
| 82 | + |
| 83 | + /// Log a warning message. |
| 84 | + /// |
| 85 | + /// Use for potentially problematic situations that aren't errors. |
| 86 | + /// |
| 87 | + /// - Parameters: |
| 88 | + /// - category: The log category for filtering |
| 89 | + /// - message: The message to log |
| 90 | + static func warning( |
| 91 | + _ category: LogCategory, |
| 92 | + _ message: String |
| 93 | + ) { |
| 94 | + log(category, level: .warning, message: message) |
| 95 | + } |
| 96 | + |
| 97 | + /// Log an error message. |
| 98 | + /// |
| 99 | + /// Use for error conditions and failures that need attention. |
| 100 | + /// |
| 101 | + /// - Parameters: |
| 102 | + /// - category: The log category for filtering |
| 103 | + /// - message: The message to log |
| 104 | + static func error( |
| 105 | + _ category: LogCategory, |
| 106 | + _ message: String |
| 107 | + ) { |
| 108 | + log(category, level: .error, message: message) |
| 109 | + } |
| 110 | + |
| 111 | + /// Log a fault message for critical system errors. |
| 112 | + /// |
| 113 | + /// Use for serious errors that may cause data loss or application instability. |
| 114 | + /// |
| 115 | + /// - Parameters: |
| 116 | + /// - category: The log category for filtering |
| 117 | + /// - message: The message to log |
| 118 | + static func fault( |
| 119 | + _ category: LogCategory, |
| 120 | + _ message: @autoclosure () -> String |
| 121 | + ) { |
| 122 | + log(category, level: .fault, message: message()) |
| 123 | + } |
| 124 | +} |
0 commit comments