Skip to content

Commit 6d3eace

Browse files
author
Guilherme Souza
committed
Start adding Log Manager class that will manage logging to disk and then submiting to supabase
1 parent c7faea7 commit 6d3eace

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PackageDescription
55

66
let package = Package(
77
name: "swift-log-supabase",
8+
platforms: [.iOS(.v13), .macOS(.v10_15)],
89
products: [
910
.library(
1011
name: "SupabaseLogger",

Sources/SupabaseLogger/SupabaseLogHandler.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Foundation
12
import Logging
23

34
public struct SupabaseLogHandler: LogHandler {
@@ -11,6 +12,12 @@ public struct SupabaseLogHandler: LogHandler {
1112
set { metadata[key] = newValue }
1213
}
1314

15+
private let logManager: SupabaseLogManager
16+
17+
public init() {
18+
logManager = SupabaseLogManager()
19+
}
20+
1421
public func log(
1522
level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, source: String,
1623
file: String, function: String, line: UInt
@@ -23,5 +30,24 @@ public struct SupabaseLogHandler: LogHandler {
2330
parameters["line"] = .stringConvertible(line)
2431
parameters["source"] = .string(source)
2532
parameters["function"] = .string(function)
33+
34+
var payload: [String: Any] = [:]
35+
payload["level"] = level.rawValue
36+
payload["message"] = metadata?.description
37+
payload["metadata"] = parameters.mapValues(\.description)
38+
39+
logManager.log(payload)
40+
}
41+
}
42+
43+
final class SupabaseLogManager {
44+
45+
let queue = DispatchQueue(label: "co.binaryscraping.supabase-log-manager", qos: .background)
46+
var payloads: [[String: Any]] = []
47+
48+
func log(_ payload: [String: Any]) {
49+
queue.async {
50+
self.payloads.append(payload)
51+
}
2652
}
2753
}

Tests/SupabaseLoggerTests/swift_log_supabaseTests.swift renamed to Tests/SupabaseLoggerTests/SupabaseLogHandlerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import XCTest
22

33
@testable import SupabaseLogger
44

5-
final class swift_log_supabaseTests: XCTestCase {
5+
final class SupabaseLogHandlerTests: XCTestCase {
66
func testExample() throws {
77
}
88
}

supabase-init.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
create table logs (
2+
id uuid default uuid_generate_v4() not null primary key,
3+
level text not null,
4+
message text,
5+
logged_at timestamp with time zone not null,
6+
received_at timestamp with time zone default timezone('utc'::text, now()) not null,
7+
metadata jsonb not null
8+
);

0 commit comments

Comments
 (0)