Skip to content

Commit

Permalink
Add SupabaseLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jan 15, 2024
1 parent 01dd25a commit da6dcdd
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 121 deletions.
19 changes: 13 additions & 6 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public actor AuthClient {
public var headers: [String: String]
public let flowType: AuthFlowType
public let localStorage: AuthLocalStorage
public let logHandler: SupabaseLogHandler
public let encoder: JSONEncoder
public let decoder: JSONDecoder
public let fetch: FetchHandler
Expand All @@ -27,6 +28,7 @@ public actor AuthClient {
/// - headers: Custom headers to be included in requests.
/// - flowType: The authentication flow type.
/// - localStorage: The storage mechanism for local data.
/// - logHandler: The LogHandler to use.
/// - encoder: The JSON encoder to use for encoding requests.
/// - decoder: The JSON decoder to use for decoding responses.
/// - fetch: The asynchronous fetch handler for network requests.
Expand All @@ -35,6 +37,7 @@ public actor AuthClient {
headers: [String: String] = [:],
flowType: AuthFlowType = Configuration.defaultFlowType,
localStorage: AuthLocalStorage,
logHandler: SupabaseLogHandler = DefaultSupabaseLogHandler.shared,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
Expand All @@ -45,6 +48,7 @@ public actor AuthClient {
self.headers = headers
self.flowType = flowType
self.localStorage = localStorage
self.logHandler = logHandler
self.encoder = encoder
self.decoder = decoder
self.fetch = fetch
Expand Down Expand Up @@ -106,6 +110,7 @@ public actor AuthClient {
headers: [String: String] = [:],
flowType: AuthFlowType = AuthClient.Configuration.defaultFlowType,
localStorage: AuthLocalStorage,
logHandler: SupabaseLogHandler = DefaultSupabaseLogHandler.shared,
encoder: JSONEncoder = AuthClient.Configuration.jsonEncoder,
decoder: JSONDecoder = AuthClient.Configuration.jsonDecoder,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) }
Expand All @@ -116,6 +121,7 @@ public actor AuthClient {
headers: headers,
flowType: flowType,
localStorage: localStorage,
logHandler: logHandler,
encoder: encoder,
decoder: decoder,
fetch: fetch
Expand All @@ -136,7 +142,8 @@ public actor AuthClient {
codeVerifierStorage: .live,
api: api,
eventEmitter: .live,
sessionStorage: .live
sessionStorage: .live,
logger: SupabaseLogger(system: "AuthClient", handler: configuration.logHandler)
)
}

Expand All @@ -147,7 +154,8 @@ public actor AuthClient {
codeVerifierStorage: CodeVerifierStorage,
api: APIClient,
eventEmitter: EventEmitter,
sessionStorage: SessionStorage
sessionStorage: SessionStorage,
logger: SupabaseLogger
) {
mfa = AuthMFA()

Expand All @@ -164,7 +172,7 @@ public actor AuthClient {
}
),
codeVerifierStorage: codeVerifierStorage,
logger: SupabaseLogger(system: "GoTrue")
logger: logger
)
)
}
Expand All @@ -177,11 +185,11 @@ public actor AuthClient {
session: Session?
)> {
let (id, stream) = eventEmitter.attachListener()
logger.log(.debug, message: "auth state change listener with id '\(id.uuidString)' attached.")
logger.debug("auth state change listener with id '\(id.uuidString)' attached.")

Task { [id] in
await emitInitialSession(forStreamWithID: id)
logger.log(.debug, message: "initial session for listener with id '\(id.uuidString)' emitted.")
logger.debug("initial session for listener with id '\(id.uuidString)' emitted.")
}

return stream
Expand Down Expand Up @@ -873,7 +881,6 @@ public actor AuthClient {

private func prepareForPKCE() -> (codeChallenge: String?, codeChallengeMethod: String?) {
if configuration.flowType == .pkce {

let codeVerifier = PKCE.generateCodeVerifier()

do {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Internal/Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _Helpers
import ConcurrencyExtras
import Foundation
import _Helpers

struct Dependencies: Sendable {
static let current = LockIsolated(Dependencies?.none)
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class PostgrestBuilder: @unchecked Sendable {
do {
return try configuration.decoder.decode(T.self, from: data)
} catch {
debug("Fail to decode type '\(T.self) with error: \(error)")
configuration.logger.error("Fail to decode type '\(T.self) with error: \(error)")
throw error
}
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public actor PostgrestClient {
public var encoder: JSONEncoder
public var decoder: JSONDecoder

let logger: SupabaseLogger

/// Initializes a new configuration for the PostgREST client.
/// - Parameters:
/// - url: The URL of the PostgREST server.
Expand All @@ -32,6 +34,7 @@ public actor PostgrestClient {
url: URL,
schema: String? = nil,
headers: [String: String] = [:],
logHandler: SupabaseLogHandler = DefaultSupabaseLogHandler.shared,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) },
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
decoder: JSONDecoder = PostgrestClient.Configuration.jsonDecoder
Expand All @@ -42,6 +45,8 @@ public actor PostgrestClient {
self.fetch = fetch
self.encoder = encoder
self.decoder = decoder

logger = SupabaseLogger(system: "Postgrest", handler: logHandler)
}
}

Expand All @@ -67,6 +72,7 @@ public actor PostgrestClient {
url: URL,
schema: String? = nil,
headers: [String: String] = [:],
logHandler: SupabaseLogHandler = DefaultSupabaseLogHandler.shared,
fetch: @escaping FetchHandler = { try await URLSession.shared.data(for: $0) },
encoder: JSONEncoder = PostgrestClient.Configuration.jsonEncoder,
decoder: JSONDecoder = PostgrestClient.Configuration.jsonDecoder
Expand All @@ -76,6 +82,7 @@ public actor PostgrestClient {
url: url,
schema: schema,
headers: headers,
logHandler: logHandler,
fetch: fetch,
encoder: encoder,
decoder: decoder
Expand Down
2 changes: 2 additions & 0 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class SupabaseClient: @unchecked Sendable {
url: databaseURL,
schema: options.db.schema,
headers: defaultHeaders,
logHandler: options.global.logHandler,
fetch: fetchWithAuth,
encoder: options.db.encoder,
decoder: options.db.decoder
Expand Down Expand Up @@ -93,6 +94,7 @@ public final class SupabaseClient: @unchecked Sendable {
headers: defaultHeaders,
flowType: options.auth.flowType,
localStorage: options.auth.storage,
logHandler: options.global.logHandler,
encoder: options.auth.encoder,
decoder: options.auth.decoder,
fetch: {
Expand Down
11 changes: 10 additions & 1 deletion Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _Helpers
import Auth
import Foundation
import PostgREST
Expand Down Expand Up @@ -67,9 +68,17 @@ public struct SupabaseClientOptions: Sendable {
/// A session to use for making requests, defaults to `URLSession.shared`.
public let session: URLSession

public init(headers: [String: String] = [:], session: URLSession = .shared) {
/// The log handler instance to use across all Supabase sub-packages.
public let logHandler: SupabaseLogHandler

public init(
headers: [String: String] = [:],
session: URLSession = .shared,
logHandler: SupabaseLogHandler = DefaultSupabaseLogHandler.shared
) {
self.headers = headers
self.session = session
self.logHandler = logHandler
}
}

Expand Down
36 changes: 0 additions & 36 deletions Sources/_Helpers/Logger.swift

This file was deleted.

76 changes: 0 additions & 76 deletions Sources/_Helpers/SupabaseLogger.swift

This file was deleted.

32 changes: 32 additions & 0 deletions Sources/_Helpers/SupabaseLogger/Entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Entry.swift
//
//
// Created by Guilherme Souza on 15/01/24.
//

import Foundation

extension SupabaseLogger {
public struct Entry: Codable, CustomStringConvertible {
public let system: String
public let level: Level
public let message: String
public let fileID: String
public let function: String
public let line: UInt
public let timestamp: TimeInterval

public var description: String {
let date = iso8601Formatter.string(from: Date(timeIntervalSince1970: timestamp))
let file = fileID.split(separator: ".", maxSplits: 1).first.map(String.init) ?? fileID
return "\(date) [\(level)] [\(system)] [\(file).\(function):\(line)] \(message)"
}
}
}

private let iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime]
return formatter
}()
27 changes: 27 additions & 0 deletions Sources/_Helpers/SupabaseLogger/Level.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Level.swift
//
//
// Created by Guilherme Souza on 15/01/24.
//

import Foundation

extension SupabaseLogger {
public enum Level: Int, Codable, CustomStringConvertible, Sendable {
case debug
case warning
case error

public var description: String {
switch self {
case .debug:
"debug"
case .warning:
"warning"
case .error:
"error"
}
}
}
}
Loading

0 comments on commit da6dcdd

Please sign in to comment.