Skip to content

Commit

Permalink
Logger can add a sink (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 14, 2024
1 parent 64d0cef commit 18091ea
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions Sources/BulkLogger/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Logger: Sendable {

// MARK: - Properties

private let sinks: [any BulkSinkType<LogData>]
private nonisolated(unsafe) var sinks: [any BulkSinkType<LogData>]
private nonisolated(unsafe) var _isEnabled: Bool = true
private let lock = NSLock()

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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<LogData>) {
lock.lock()
defer {
lock.unlock()
}
sinks.append(sink)
}

public func makeContextualLogger(context: String) -> Logger {
return .init(context: context, source: self)
}
Expand Down

0 comments on commit 18091ea

Please sign in to comment.