|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Logging |
| 16 | +import Synchronization |
| 17 | +import Testing |
| 18 | + |
| 19 | +struct CollectEverythingLogHandler: LogHandler { |
| 20 | + var metadata: Logger.Metadata = [:] |
| 21 | + var logLevel: Logger.Level = .info |
| 22 | + let logStore: LogStore |
| 23 | + |
| 24 | + final class LogStore: Sendable { |
| 25 | + struct Entry: Sendable { |
| 26 | + var level: Logger.Level |
| 27 | + var message: String |
| 28 | + var metadata: [String: String] |
| 29 | + } |
| 30 | + |
| 31 | + let logs: Mutex<[Entry]> = .init([]) |
| 32 | + |
| 33 | + func append(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?) { |
| 34 | + self.logs.withLock { entries in |
| 35 | + entries.append( |
| 36 | + Entry( |
| 37 | + level: level, |
| 38 | + message: message.description, |
| 39 | + metadata: metadata?.mapValues { $0.description } ?? [:] |
| 40 | + ) |
| 41 | + ) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + func clear() { |
| 46 | + self.logs.withLock { |
| 47 | + $0.removeAll() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + enum LogFieldExpectedValue: ExpressibleByStringLiteral, ExpressibleByStringInterpolation { |
| 52 | + case exactMatch(String) |
| 53 | + case beginsWith(String) |
| 54 | + case wildcard |
| 55 | + case predicate((String) -> Bool) |
| 56 | + |
| 57 | + init(stringLiteral value: String) { |
| 58 | + self = .exactMatch(value) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @discardableResult |
| 63 | + func assertContainsLog( |
| 64 | + _ message: String, |
| 65 | + _ metadata: (String, LogFieldExpectedValue)..., |
| 66 | + sourceLocation: SourceLocation = #_sourceLocation |
| 67 | + ) -> [Entry] { |
| 68 | + var candidates = self.getAllLogsWithMessage(message) |
| 69 | + if candidates.isEmpty { |
| 70 | + Issue.record("Logs do not contain entry with message: \(message)", sourceLocation: sourceLocation) |
| 71 | + return [] |
| 72 | + } |
| 73 | + for (key, value) in metadata { |
| 74 | + var errorMsg: String |
| 75 | + switch value { |
| 76 | + case .wildcard: |
| 77 | + candidates = candidates.filter { $0.metadata.contains { $0.key == key } } |
| 78 | + errorMsg = "Logs do not contain entry with message: \(message) and metadata: \(key) *" |
| 79 | + case .predicate(let predicate): |
| 80 | + candidates = candidates.filter { $0.metadata[key].map(predicate) ?? false } |
| 81 | + errorMsg = |
| 82 | + "Logs do not contain entry with message: \(message) and metadata: \(key) matching predicate" |
| 83 | + case .beginsWith(let prefix): |
| 84 | + candidates = candidates.filter { $0.metadata[key]?.hasPrefix(prefix) ?? false } |
| 85 | + errorMsg = "Logs do not contain entry with message: \(message) and metadata: \(key), \(value)" |
| 86 | + case .exactMatch(let value): |
| 87 | + candidates = candidates.filter { $0.metadata[key] == value } |
| 88 | + errorMsg = "Logs do not contain entry with message: \(message) and metadata: \(key), \(value)" |
| 89 | + } |
| 90 | + if candidates.isEmpty { |
| 91 | + Issue.record("Error: \(errorMsg)", sourceLocation: sourceLocation) |
| 92 | + return [] |
| 93 | + } |
| 94 | + } |
| 95 | + return candidates |
| 96 | + } |
| 97 | + |
| 98 | + func assertDoesNotContainMessage(_ message: String, sourceLocation: SourceLocation = #_sourceLocation) { |
| 99 | + let candidates = self.getAllLogsWithMessage(message) |
| 100 | + if candidates.count > 0 { |
| 101 | + Issue.record("Logs contain entry with message: \(message)", sourceLocation: sourceLocation) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + func getAllLogs() -> [Entry] { |
| 106 | + self.logs.withLock { $0 } |
| 107 | + } |
| 108 | + |
| 109 | + func getAllLogsWithMessage(_ message: String) -> [Entry] { |
| 110 | + self.getAllLogs().filter { $0.message == message } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + init(logStore: LogStore) { |
| 115 | + self.logStore = logStore |
| 116 | + } |
| 117 | + |
| 118 | + func log( |
| 119 | + level: Logger.Level, |
| 120 | + message: Logger.Message, |
| 121 | + metadata: Logger.Metadata?, |
| 122 | + source: String, |
| 123 | + file: String, |
| 124 | + function: String, |
| 125 | + line: UInt |
| 126 | + ) { |
| 127 | + self.logStore.append(level: level, message: message, metadata: self.metadata.merging(metadata ?? [:]) { $1 }) |
| 128 | + } |
| 129 | + |
| 130 | + subscript(metadataKey key: String) -> Logger.Metadata.Value? { |
| 131 | + get { |
| 132 | + self.metadata[key] |
| 133 | + } |
| 134 | + set { |
| 135 | + self.metadata[key] = newValue |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments