@@ -55,9 +55,13 @@ public class PrintLogWriter: LogWriterProtocol {
5555}
5656
5757/// A default logger configuration that uses `PrintLogWriter` and filters messages by minimum severity.
58- public final class DefaultLogger : LoggerProtocol , @unchecked Sendable {
59- var minSeverity : LogSeverity
60- var writers : [ any LogWriterProtocol ]
58+ public final class DefaultLogger : LoggerProtocol ,
59+ // The shared state is guarded by the DispatchQueue
60+ @unchecked Sendable
61+ {
62+ private var minSeverity : LogSeverity
63+ private var writers : [ any LogWriterProtocol ]
64+ private let queue = DispatchQueue ( label: " DefaultLogger.queue " )
6165
6266 /// Initializes the default logger with an optional minimum severity level.
6367 ///
@@ -70,35 +74,41 @@ public final class DefaultLogger: LoggerProtocol, @unchecked Sendable {
7074 }
7175
7276 public func setWriters( _ writers: [ any LogWriterProtocol ] ) {
73- self . writers = writers
77+ queue. sync {
78+ self . writers = writers
79+ }
7480 }
7581
7682 public func setMinSeverity( _ severity: LogSeverity ) {
77- minSeverity = severity
83+ queue. sync {
84+ minSeverity = severity
85+ }
7886 }
7987
80- public func debug( _ message: String , tag: String ? = nil ) {
88+ public nonisolated func debug( _ message: String , tag: String ? = nil ) {
8189 writeLog ( message, severity: LogSeverity . debug, tag: tag)
8290 }
8391
84- public func error( _ message: String , tag: String ? = nil ) {
92+ public nonisolated func error( _ message: String , tag: String ? = nil ) {
8593 writeLog ( message, severity: LogSeverity . error, tag: tag)
8694 }
8795
88- public func info( _ message: String , tag: String ? = nil ) {
96+ public nonisolated func info( _ message: String , tag: String ? = nil ) {
8997 writeLog ( message, severity: LogSeverity . info, tag: tag)
9098 }
9199
92- public func warning( _ message: String , tag: String ? = nil ) {
100+ public nonisolated func warning( _ message: String , tag: String ? = nil ) {
93101 writeLog ( message, severity: LogSeverity . warning, tag: tag)
94102 }
95103
96- public func fault( _ message: String , tag: String ? = nil ) {
104+ public nonisolated func fault( _ message: String , tag: String ? = nil ) {
97105 writeLog ( message, severity: LogSeverity . fault, tag: tag)
98106 }
99107
100- private func writeLog( _ message: String , severity: LogSeverity , tag: String ? ) {
101- if severity. rawValue < minSeverity. rawValue {
108+ private nonisolated func writeLog( _ message: String , severity: LogSeverity , tag: String ? ) {
109+ let currentSeverity = queue. sync { minSeverity }
110+
111+ if severity. rawValue < currentSeverity. rawValue {
102112 return
103113 }
104114
0 commit comments