diff --git a/Sources/BulkLogger/Logger.swift b/Sources/BulkLogger/Logger.swift index f88752c..0acf2ce 100644 --- a/Sources/BulkLogger/Logger.swift +++ b/Sources/BulkLogger/Logger.swift @@ -42,7 +42,7 @@ public final class Logger: Sendable { // MARK: - Properties - private let sinks: [any BulkSinkType] + private nonisolated(unsafe) var sinks: [any BulkSinkType] private nonisolated(unsafe) var _isEnabled: Bool = true private let lock = NSLock() @@ -90,9 +90,15 @@ public final class Logger: Sendable { function: function.description, line: line ) - - Task { [sinks] in - for sink in sinks { + + lock.lock() + + let usingSinks = self.sinks + + lock.unlock() + + Task { [usingSinks] in + for sink in usingSinks { await sink.send(log) } } @@ -164,6 +170,14 @@ public final class Logger: Sendable { _write(level: .error, items, file: file, function: function, line: line, dsoHandle: dsoHandle) } + public func addSink(_ sink: any BulkSinkType) { + lock.lock() + defer { + lock.unlock() + } + sinks.append(sink) + } + public func makeContextualLogger(context: String) -> Logger { return .init(context: context, source: self) }