Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/StreamCore/Logger/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public enum LogConfig {
}

/// Entity used for logging messages.
open class Logger {
open class Logger: @unchecked Sendable {
/// Identifier of the Logger. Will be visible if a destination has `showIdentifiers` enabled.
public let identifier: String

Expand Down
103 changes: 103 additions & 0 deletions Tests/StreamCoreTests/Logger/Logger_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@
// Copyright © 2026 Stream.io Inc. All rights reserved.
//

import Foundation
@testable import StreamCore
import Testing

struct Logger_Tests {
@Test func concurrentLoggingProcessesAllMessages() async throws {
let iterations = 200
let destination = CapturingDestination()
let logger = Logger(identifier: "test", destinations: [destination])

await withTaskGroup(of: Void.self) { group in
for index in 0..<iterations {
group.addTask {
logger.debug("message_\(index)")
}
}
}

let deadline = Date().addingTimeInterval(5)
while destination.processedCount < iterations, Date() < deadline {
try await Task.sleep(nanoseconds: 10_000_000)
}

#expect(destination.processedCount == iterations)
#expect(
destination.recordedThreadNames.contains {
$0.contains("LoggerQueue")
} == false
)
}

// MARK: - Concurrent Logger Invalidation Tests

@Test func concurrentLoggerInvalidation() async throws {
Expand Down Expand Up @@ -141,3 +168,79 @@ struct Logger_Tests {
LogConfig.reset()
}
}

private final class CapturingDestination: BaseLogDestination, @unchecked Sendable {
private var logDetails: [LogDetails] = []
private let lock = NSLock()

init() {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
super.init(
identifier: UUID().uuidString,
level: .debug,
subsystems: .all,
showDate: false,
dateFormatter: formatter,
formatters: [],
showLevel: false,
showIdentifier: false,
showThreadName: true,
showFileName: false,
showLineNumber: false,
showFunctionName: false
)
}

@available(*, unavailable)
required init(
identifier: String,
level: LogLevel,
subsystems: LogSubsystem,
showDate: Bool,
dateFormatter: DateFormatter,
formatters: [LogFormatter],
showLevel: Bool,
showIdentifier: Bool,
showThreadName: Bool,
showFileName: Bool,
showLineNumber: Bool,
showFunctionName: Bool
) {
super.init(
identifier: identifier,
level: level,
subsystems: subsystems,
showDate: showDate,
dateFormatter: dateFormatter,
formatters: formatters,
showLevel: showLevel,
showIdentifier: showIdentifier,
showThreadName: showThreadName,
showFileName: showFileName,
showLineNumber: showLineNumber,
showFunctionName: showFunctionName
)
fatalError("Unsupported initializer")
}

override func process(logDetails: LogDetails) {
lock.lock()
self.logDetails.append(logDetails)
lock.unlock()
}

override func write(message: String) {}

var processedCount: Int {
lock.lock()
defer { lock.unlock() }
return logDetails.count
}

var recordedThreadNames: [String] {
lock.lock()
defer { lock.unlock() }
return logDetails.map(\.threadName)
}
}
Loading