@@ -16,7 +16,11 @@ import Foundation
1616public final class LoggingScope {
1717
1818 /// The name of the default logging subsystem if no task-local value is set.
19- fileprivate static let defaultSubsystem : ThreadSafeBox < String ? > = . init( initialValue: nil )
19+ private static let defaultSubsystem = ThreadSafeBox < String ? > ( initialValue: nil )
20+
21+ /// Whether we have logged a fault that `subsystem` has been accessed without calling
22+ /// `configureDefaultLoggingSubsystem` first.
23+ private static let hasLoggedNoSubsystemConfiguredFault = AtomicBool ( initialValue: false )
2024
2125 /// The name of the current logging subsystem or `nil` if no logging scope is set.
2226 @TaskLocal fileprivate static var _subsystem : String ?
@@ -31,9 +35,16 @@ public final class LoggingScope {
3135 } else if let defaultSubsystem = defaultSubsystem. value {
3236 return defaultSubsystem
3337 } else {
34- Logger ( subsystem: " org.swift.sklogging " , category: " configuration " )
35- . log ( level: . fault, " default logging subsystem was not configured before first use " )
36- return " org.swift "
38+ if !hasLoggedNoSubsystemConfiguredFault. setAndGet ( newValue: true ) {
39+ Logger ( subsystem: " default " , category: " sklogging " )
40+ . fault (
41+ """
42+ Default logging subsystem was not configured before first use of SKLogging. \
43+ Ensure that configureDefaultLoggingSubsystem is called before the first log call.
44+ """
45+ )
46+ }
47+ return " default "
3748 }
3849 }
3950
@@ -42,8 +53,16 @@ public final class LoggingScope {
4253 return _scope ?? " default "
4354 }
4455
56+ /// Set the logging subsystem that is used task local subsystem is set using `withLoggingSubsystemAndScope`.
57+ ///
58+ /// Must be called before the first log call.
4559 public static func configureDefaultLoggingSubsystem( _ subsystem: String ) {
46- LoggingScope . defaultSubsystem. withLock { $0 = subsystem }
60+ LoggingScope . defaultSubsystem. withLock { defaultSubsystem in
61+ if let defaultSubsystem, defaultSubsystem != subsystem {
62+ logger. log ( " Changing default log subsystem from \( defaultSubsystem) to \( subsystem) " )
63+ }
64+ defaultSubsystem = subsystem
65+ }
4766 }
4867}
4968
0 commit comments