Skip to content

Commit 7e9325b

Browse files
optional tag usage
1 parent 1580762 commit 7e9325b

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

Sources/PowerSync/Kotlin/DatabaseLogger.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private class KermitLogWriterAdapter: Kermit_coreLogWriter {
99

1010
/// Initializes a new adapter.
1111
///
12-
/// - Parameter adapter: A Swift log writer that will handle log output.
12+
/// - Parameter logger: A Swift log writer that will handle log output.
1313
init(logger: any LoggerProtocol) {
1414
self.logger = logger
1515
super.init()
@@ -20,7 +20,7 @@ private class KermitLogWriterAdapter: Kermit_coreLogWriter {
2020
/// - Parameters:
2121
/// - severity: The severity level of the log.
2222
/// - message: The content of the log message.
23-
/// - tag: An optional string categorizing the log.
23+
/// - tag: A string categorizing the log.
2424
/// - throwable: An optional Kotlin exception (ignored here).
2525
override func log(severity: Kermit_coreSeverity, message: String, tag: String, throwable: KotlinThrowable?) {
2626
switch severity {
@@ -40,7 +40,7 @@ private class KermitLogWriterAdapter: Kermit_coreLogWriter {
4040
}
4141
}
4242

43-
/// A logger implementation that integrates with PowerSync's Kotlin backend using Kermit.
43+
/// A logger implementation that integrates with PowerSync's Kotlin core using Kermit.
4444
///
4545
/// This class bridges Swift log writers with the Kotlin logging system and supports
4646
/// runtime configuration of severity levels and writer lists.
@@ -51,10 +51,10 @@ internal class DatabaseLogger: LoggerProtocol {
5151

5252
/// Initializes a new logger with an optional list of writers.
5353
///
54-
/// - Parameter writers: An array of Swift log writers. Defaults to an empty array.
54+
/// - Parameter logger: A logger which will be called for each internal log operation
5555
init(_ logger: any LoggerProtocol) {
5656
self.logger = logger
57-
// Set to the lowest severity. The adapter downstream logger should filter by severity
57+
// Set to the lowest severity. The provided logger should filter by severity
5858
kLogger.mutableConfig.setMinSeverity(Kermit_coreSeverity.verbose)
5959
kLogger.mutableConfig.setLogWriterList(
6060
[KermitLogWriterAdapter(logger: logger)]
@@ -80,27 +80,27 @@ internal class DatabaseLogger: LoggerProtocol {
8080
}
8181

8282
/// Logs a debug-level message.
83-
public func debug(_ message: String, tag: String) {
83+
public func debug(_ message: String, tag: String?) {
8484
logger.debug(message, tag: tag)
8585
}
8686

8787
/// Logs an info-level message.
88-
public func info(_ message: String, tag: String) {
88+
public func info(_ message: String, tag: String?) {
8989
logger.info(message, tag: tag)
9090
}
9191

9292
/// Logs a warning-level message.
93-
public func warning(_ message: String, tag: String) {
93+
public func warning(_ message: String, tag: String?) {
9494
logger.warning(message, tag: tag)
9595
}
9696

9797
/// Logs an error-level message.
98-
public func error(_ message: String, tag: String) {
98+
public func error(_ message: String, tag: String?) {
9999
logger.error(message, tag: tag)
100100
}
101101

102102
/// Logs a fault (assert-level) message, typically used for critical issues.
103-
public func fault(_ message: String, tag: String) {
103+
public func fault(_ message: String, tag: String?) {
104104
logger.fault(message, tag: tag)
105105
}
106106
}

Sources/PowerSync/Logger.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ public class PrintLogWriter: LogWriterProtocol {
1111
/// - severity: The severity level of the message.
1212
/// - message: The content of the log message.
1313
/// - tag: An optional tag used to categorize the message. If empty, no brackets are shown.
14-
public func log(severity: LogSeverity, message: String, tag: String) {
15-
let tagPrefix = tag.isEmpty ? "" : "[\(tag)] "
14+
public func log(severity: LogSeverity, message: String, tag: String?) {
15+
let tagPrefix: String
16+
if let tag, !tag.isEmpty {
17+
tagPrefix = "[\(tag)] "
18+
} else {
19+
tagPrefix = ""
20+
}
21+
1622
let message = "\(tagPrefix) \(message)"
1723
if #available(iOS 14.0, *) {
1824
let l = Logger()
@@ -30,21 +36,21 @@ public class PrintLogWriter: LogWriterProtocol {
3036
l.fault("\(message)")
3137
}
3238
} else {
33-
print("\(severity.rawValue): \(message)")
39+
print("\(severity.stringValue): \(message)")
3440
}
3541
}
3642
}
3743

38-
/// A default logger configuration that uses `SwiftLogWritter` and filters messages by minimum severity.
39-
///
40-
/// This logger integrates with your custom logging system and uses `os.Logger` under the hood.
44+
/// A default logger configuration that uses `PrintLogWritter` and filters messages by minimum severity.
4145
public class DefaultLogger: LoggerProtocol {
4246
public var minSeverirty: LogSeverity
4347
public var writers: [any LogWriterProtocol]
4448

4549
/// Initializes the default logger with an optional minimum severity level.
4650
///
47-
/// - Parameter minSeverity: The minimum severity level to log. Defaults to `.debug`.
51+
/// - Parameters
52+
/// - minSeverity: The minimum severity level to log. Defaults to `.debug`.
53+
/// - writers: Optional writers which logs should be written to. Defaults to a `PrintLogWriter`.
4854
public init(minSeverity: LogSeverity = .debug, writers: [any LogWriterProtocol]? = nil ) {
4955
self.writers = writers ?? [ PrintLogWriter() ]
5056
self.minSeverirty = minSeverity
@@ -59,27 +65,27 @@ public class DefaultLogger: LoggerProtocol {
5965
}
6066

6167

62-
public func debug(_ message: String, tag: String) {
63-
self.writeLog(message, tag: tag, severity: LogSeverity.debug)
68+
public func debug(_ message: String, tag: String? = nil) {
69+
self.writeLog(message, severity: LogSeverity.debug, tag: tag)
6470
}
6571

66-
public func error(_ message: String, tag: String) {
67-
self.writeLog(message, tag: tag, severity: LogSeverity.error)
72+
public func error(_ message: String, tag: String? = nil) {
73+
self.writeLog(message, severity: LogSeverity.error, tag: tag)
6874
}
6975

70-
public func info(_ message: String, tag: String) {
71-
self.writeLog(message, tag: tag, severity: LogSeverity.info)
76+
public func info(_ message: String, tag: String? = nil) {
77+
self.writeLog(message, severity: LogSeverity.info, tag: tag)
7278
}
7379

74-
public func warning(_ message: String, tag: String) {
75-
self.writeLog(message, tag: tag, severity: LogSeverity.warning)
80+
public func warning(_ message: String, tag: String? = nil) {
81+
self.writeLog(message, severity: LogSeverity.warning, tag: tag)
7682
}
7783

78-
public func fault(_ message: String, tag: String) {
79-
self.writeLog(message, tag: tag, severity: LogSeverity.fault)
84+
public func fault(_ message: String, tag: String? = nil) {
85+
self.writeLog(message, severity: LogSeverity.fault, tag: tag)
8086
}
8187

82-
private func writeLog(_ message: String, tag: String, severity: LogSeverity) {
88+
private func writeLog(_ message: String, severity: LogSeverity, tag: String?) {
8389
if (severity.rawValue < self.minSeverirty.rawValue) {
8490
return
8591
}

Sources/PowerSync/LoggerProtocol.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public protocol LogWriterProtocol {
4141
/// - severity: The severity level of the log message.
4242
/// - message: The content of the log message.
4343
/// - tag: An optional tag to categorize or group the log message.
44-
func log(severity: LogSeverity, message: String, tag: String)
44+
func log(severity: LogSeverity, message: String, tag: String?)
4545
}
4646

4747
/// A protocol defining the interface for a logger that supports severity filtering and multiple writers.
@@ -65,33 +65,33 @@ public protocol LoggerProtocol {
6565
/// - Parameters:
6666
/// - message: The content of the log message.
6767
/// - tag: An optional tag to categorize the message.
68-
func info(_ message: String, tag: String)
68+
func info(_ message: String, tag: String?)
6969

7070
/// Logs an error message.
7171
///
7272
/// - Parameters:
7373
/// - message: The content of the log message.
7474
/// - tag: An optional tag to categorize the message.
75-
func error(_ message: String, tag: String)
75+
func error(_ message: String, tag: String?)
7676

7777
/// Logs a debug message.
7878
///
7979
/// - Parameters:
8080
/// - message: The content of the log message.
8181
/// - tag: An optional tag to categorize the message.
82-
func debug(_ message: String, tag: String)
82+
func debug(_ message: String, tag: String?)
8383

8484
/// Logs a warning message.
8585
///
8686
/// - Parameters:
8787
/// - message: The content of the log message.
8888
/// - tag: An optional tag to categorize the message.
89-
func warning(_ message: String, tag: String)
89+
func warning(_ message: String, tag: String?)
9090

9191
/// Logs a fault message, typically used for critical system-level failures.
9292
///
9393
/// - Parameters:
9494
/// - message: The content of the log message.
9595
/// - tag: An optional tag to categorize the message.
96-
func fault(_ message: String, tag: String)
96+
func fault(_ message: String, tag: String?)
9797
}

Tests/PowerSyncTests/Kotlin/TestLogger.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
class TestLogWriterAdapter: LogWriterProtocol {
55
var logs = [String]()
66

7-
func log(severity: LogSeverity, message: String, tag: String) {
8-
logs.append("\(severity): \(message) (\(tag))")
7+
func log(severity: LogSeverity, message: String, tag: String?) {
8+
logs.append("\(severity): \(message) \(tag != nil ? "\(tag!)" : "")")
99
}
1010
}
1111

0 commit comments

Comments
 (0)