Skip to content

Commit d5e74e9

Browse files
authored
fix(logging): update flushlog to be blocking end to end (#3113)
* fix(logging): update flushlog to be blocking end to end * chore(logging): fix typo * chore(logging): address review comments
1 parent 3aec252 commit d5e74e9

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/AWSCloudWatchLoggingSessionController.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,22 @@ final class AWSCloudWatchLoggingSessionController {
164164
}
165165

166166
func flushLogs() async throws {
167-
try await session?.logger.flushLogs()
167+
guard let logBatches = try await session?.logger.getLogBatches() else { return }
168+
169+
for batch in logBatches {
170+
try await consumeLogBatch(batch)
171+
}
172+
}
173+
174+
private func consumeLogBatch(_ batch: LogBatch) async throws {
175+
do {
176+
try await consumer?.consume(batch: batch)
177+
} catch {
178+
Amplify.Logging.default.error("Error flushing logs with error \(error.localizedDescription)")
179+
let payload = HubPayload(eventName: HubPayload.EventName.Logging.flushLogFailure, context: error.localizedDescription)
180+
Amplify.Hub.dispatch(to: HubChannel.logging, payload: payload)
181+
try batch.complete()
182+
}
168183
}
169184

170185
private func resetCurrentLogs() {

AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ actor LogActor {
5050
try rotation.currentLogFile.synchronize()
5151
}
5252

53-
func flushLogs() throws {
54-
let logs = try rotation.getAllLogs()
55-
for log in logs {
56-
rotationSubject.send(log)
57-
}
53+
func getLogs() throws -> [URL] {
54+
return try rotation.getAllLogs()
5855
}
5956

6057
func deleteLogs() throws {

AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Producer/RotatingLogger.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ final class RotatingLogger {
4343
try await actor.synchronize()
4444
}
4545

46-
func flushLogs() async throws {
47-
try await setupSubscription()
48-
try await actor.flushLogs()
46+
func getLogBatches() async throws -> [RotatingLogBatch] {
47+
let logs = try await actor.getLogs()
48+
return logs.map({RotatingLogBatch(url: $0)})
4949
}
5050

5151
func resetLogs() async throws {

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,22 @@ final class LogActorTests: XCTestCase {
109109
XCTAssertTrue(contents.isEmpty)
110110
}
111111

112+
113+
/// Given: a LogActor with 1 existing log
114+
/// When: get all logs is called after writing and rotating to a new log
115+
/// Then: 2 log files are returned
116+
func testLogActorReturnsLogList() async throws {
117+
var logs = try await systemUnderTest.getLogs()
118+
XCTAssertEqual(logs.count, 1)
119+
let size = try LogEntry.minimumSizeForLogEntry(level: .error)
120+
let numberOfEntries = (fileSizeLimitInBytes/size) + 1
121+
let entries = (0..<numberOfEntries).map { LogEntry(category: "", namespace: nil, level: .error, message: "\($0)", created: .init(timeIntervalSince1970: Double($0))) }
122+
for entry in entries {
123+
try await systemUnderTest.record(entry)
124+
}
125+
try await systemUnderTest.synchronize()
126+
127+
logs = try await systemUnderTest.getLogs()
128+
XCTAssertEqual(logs.count, 2)
129+
}
112130
}

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/RotatingLoggerTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ final class RotatingLoggerTests: XCTestCase {
123123
try assertSingleEntryWith(level: level, message: message)
124124
}
125125

126+
/// Given: a rotating logger with 1 existing log batch
127+
/// When: get log batch is called after writing and rotating to new log file
128+
/// Then: a list of log batches with a count of 2 is returned
129+
func testRotatingLogReturnsLogBatches() async throws {
130+
var logBatches = try await systemUnderTest.getLogBatches()
131+
XCTAssertEqual(logBatches.count, 1)
132+
let size = try LogEntry.minimumSizeForLogEntry(level: .error)
133+
let recordsPerFile = (fileSizeLimitInBytes/size)
134+
for _ in 0..<recordsPerFile {
135+
try await systemUnderTest.record(level: .error, message: "Test message")
136+
}
137+
try await systemUnderTest.synchronize()
138+
logBatches = try await systemUnderTest.getLogBatches()
139+
XCTAssertEqual(logBatches.count, 2)
140+
}
141+
126142
private func logWith(level: LogLevel, message: String) async throws {
127143
switch level {
128144
case .error:

0 commit comments

Comments
 (0)