|
| 1 | +import OSLog |
| 2 | + |
| 3 | +/// A log writer which prints to the standard output |
| 4 | +/// |
| 5 | +/// This writer uses `os.Logger` on iOS/macOS/tvOS/watchOS 14+ and falls back to `print` for earlier versions. |
| 6 | +public class PrintLogWriter: LogWriterProtocol { |
| 7 | + |
| 8 | + private let subsystem: String |
| 9 | + private let category: String |
| 10 | + private lazy var logger: Any? = { |
| 11 | + if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) { |
| 12 | + return Logger(subsystem: subsystem, category: category) |
| 13 | + } |
| 14 | + return nil |
| 15 | + }() |
| 16 | + |
| 17 | + /// Creates a new PrintLogWriter |
| 18 | + /// - Parameters: |
| 19 | + /// - subsystem: The subsystem identifier (typically reverse DNS notation of your app) |
| 20 | + /// - category: The category within your subsystem |
| 21 | + public init(subsystem: String = Bundle.main.bundleIdentifier ?? "com.powersync.logger", |
| 22 | + category: String = "default") { |
| 23 | + self.subsystem = subsystem |
| 24 | + self.category = category |
| 25 | + } |
| 26 | + |
| 27 | + /// Logs a message with a given severity and optional tag. |
| 28 | + /// - Parameters: |
| 29 | + /// - severity: The severity level of the message. |
| 30 | + /// - message: The content of the log message. |
| 31 | + /// - tag: An optional tag used to categorize the message. If empty, no brackets are shown. |
| 32 | + public func log(severity: LogSeverity, message: String, tag: String?) { |
| 33 | + let tagPrefix = tag.map { !$0.isEmpty ? "[\($0)] " : "" } ?? "" |
| 34 | + let formattedMessage = "\(tagPrefix)\(message)" |
| 35 | + |
| 36 | + if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) { |
| 37 | + guard let logger = logger as? Logger else { return } |
| 38 | + |
| 39 | + switch severity { |
| 40 | + case .info: |
| 41 | + logger.info("\(formattedMessage, privacy: .public)") |
| 42 | + case .error: |
| 43 | + logger.error("\(formattedMessage, privacy: .public)") |
| 44 | + case .debug: |
| 45 | + logger.debug("\(formattedMessage, privacy: .public)") |
| 46 | + case .warning: |
| 47 | + logger.warning("\(formattedMessage, privacy: .public)") |
| 48 | + case .fault: |
| 49 | + logger.fault("\(formattedMessage, privacy: .public)") |
| 50 | + } |
| 51 | + } else { |
| 52 | + print("\(severity.stringValue): \(formattedMessage)") |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +/// A default logger configuration that uses `PrintLogWritter` and filters messages by minimum severity. |
| 60 | +public class DefaultLogger: LoggerProtocol { |
| 61 | + public var minSeverity: LogSeverity |
| 62 | + public var writers: [any LogWriterProtocol] |
| 63 | + |
| 64 | + /// Initializes the default logger with an optional minimum severity level. |
| 65 | + /// |
| 66 | + /// - Parameters |
| 67 | + /// - minSeverity: The minimum severity level to log. Defaults to `.debug`. |
| 68 | + /// - writers: Optional writers which logs should be written to. Defaults to a `PrintLogWriter`. |
| 69 | + public init(minSeverity: LogSeverity = .debug, writers: [any LogWriterProtocol]? = nil ) { |
| 70 | + self.writers = writers ?? [ PrintLogWriter() ] |
| 71 | + self.minSeverity = minSeverity |
| 72 | + } |
| 73 | + |
| 74 | + public func setWriters(_ writters: [any LogWriterProtocol]) { |
| 75 | + self.writers = writters |
| 76 | + } |
| 77 | + |
| 78 | + public func setMinSeverity(_ severity: LogSeverity) { |
| 79 | + self.minSeverity = severity |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public func debug(_ message: String, tag: String? = nil) { |
| 84 | + self.writeLog(message, severity: LogSeverity.debug, tag: tag) |
| 85 | + } |
| 86 | + |
| 87 | + public func error(_ message: String, tag: String? = nil) { |
| 88 | + self.writeLog(message, severity: LogSeverity.error, tag: tag) |
| 89 | + } |
| 90 | + |
| 91 | + public func info(_ message: String, tag: String? = nil) { |
| 92 | + self.writeLog(message, severity: LogSeverity.info, tag: tag) |
| 93 | + } |
| 94 | + |
| 95 | + public func warning(_ message: String, tag: String? = nil) { |
| 96 | + self.writeLog(message, severity: LogSeverity.warning, tag: tag) |
| 97 | + } |
| 98 | + |
| 99 | + public func fault(_ message: String, tag: String? = nil) { |
| 100 | + self.writeLog(message, severity: LogSeverity.fault, tag: tag) |
| 101 | + } |
| 102 | + |
| 103 | + private func writeLog(_ message: String, severity: LogSeverity, tag: String?) { |
| 104 | + if (severity.rawValue < self.minSeverity.rawValue) { |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + for writer in self.writers { |
| 109 | + writer.log(severity: severity, message: message, tag: tag) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments