Skip to content

Commit c067c52

Browse files
authored
feat: add OSLogSupabaseLogger type (#757)
1 parent 34a70ac commit c067c52

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Foundation
2+
3+
#if canImport(OSLog)
4+
import OSLog
5+
6+
/// A SupabaseLogger implementation that logs to OSLog.
7+
///
8+
/// This logger maps Supabase log levels to appropriate OSLog levels:
9+
/// - `.verbose` → `.info`
10+
/// - `.debug` → `.debug`
11+
/// - `.warning` → `.notice`
12+
/// - `.error` → `.error`
13+
///
14+
/// ## Usage
15+
///
16+
/// ```swift
17+
/// let supabaseLogger = OSLogSupabaseLogger()
18+
///
19+
/// // Use with Supabase client
20+
/// let supabase = SupabaseClient(
21+
/// supabaseURL: url,
22+
/// supabaseKey: key,
23+
/// options: .init(global: .init(logger: supabaseLogger))
24+
/// )
25+
/// ```
26+
@available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
27+
public struct OSLogSupabaseLogger: SupabaseLogger {
28+
private let logger: Logger
29+
30+
/// Creates a new OSLog-based logger with a provided Logger instance.
31+
///
32+
/// - Parameter logger: The OSLog Logger instance to use for logging.
33+
public init(
34+
_ logger: Logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "", category: "Supabase")
35+
) {
36+
self.logger = logger
37+
}
38+
39+
public func log(message: SupabaseLogMessage) {
40+
let logMessage = message.description
41+
42+
switch message.level {
43+
case .verbose:
44+
logger.info("\(logMessage, privacy: .public)")
45+
case .debug:
46+
logger.debug("\(logMessage, privacy: .public)")
47+
case .warning:
48+
logger.notice("\(logMessage, privacy: .public)")
49+
case .error:
50+
logger.error("\(logMessage, privacy: .public)")
51+
}
52+
}
53+
}
54+
#endif

Sources/Helpers/SupabaseLogger.swift renamed to Sources/Helpers/Logger/SupabaseLogger.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,4 @@ extension SupabaseLogger {
214214
}
215215
}
216216
#endif
217+

0 commit comments

Comments
 (0)