Skip to content

Commit 2855d22

Browse files
authored
fix(logging): delete logs when user identifier changes (#3110)
* fix(logging): flush logs when user identifier changes * fix(logging): add unit test * fix(logging): delete logs when identifier changes * fix(logging): update per review comment
1 parent 0f9e149 commit 2855d22

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

Diff for: AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/AWSCloudWatchLoggingSessionController.swift

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ final class AWSCloudWatchLoggingSessionController {
140140
}
141141

142142
private func userIdentifierDidChange() {
143+
resetCurrentLogs()
143144
updateSession()
144145
updateConsumer()
145146
connectProducerAndConsumer()
@@ -165,6 +166,16 @@ final class AWSCloudWatchLoggingSessionController {
165166
func flushLogs() async throws {
166167
try await session?.logger.flushLogs()
167168
}
169+
170+
private func resetCurrentLogs() {
171+
Task {
172+
do {
173+
try await session?.logger.resetLogs()
174+
} catch {
175+
Amplify.Logging.error("Error resetting logs with \(error)")
176+
}
177+
}
178+
}
168179
}
169180

170181
extension AWSCloudWatchLoggingSessionController: Logger {

Diff for: AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift

+5
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ actor LogActor {
5656
rotationSubject.send(log)
5757
}
5858
}
59+
60+
func deleteLogs() throws {
61+
try rotation.reset()
62+
try synchronize()
63+
}
5964
}

Diff for: AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Producer/RotatingLogger.swift

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ final class RotatingLogger {
4848
try await actor.flushLogs()
4949
}
5050

51+
func resetLogs() async throws {
52+
try await actor.deleteLogs()
53+
}
54+
5155
func record(level: LogLevel, message: @autoclosure () -> String) async throws {
5256
try await setupSubscription()
5357
let entry = LogEntry(category: self.category, namespace: self.namespace, level: level, message: message())

Diff for: AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingSessionControllerTests.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ final class AWSCloudWatchLoggingSessionControllerTests: XCTestCase {
2020
let mockCloudWatchLogClient = MockCloudWatchLogsClient()
2121
let mockLoggingNetworkMonitor = MockLoggingNetworkMonitor()
2222
let category = "amplifytest"
23+
var unsubscribeToken: UnsubscribeToken?
2324

2425
override func tearDown() async throws {
2526
systemUnderTest = nil
27+
if let token = unsubscribeToken {
28+
Amplify.Hub.removeListener(token)
29+
}
2630
let file = getLogFile()
2731
do {
2832
try FileManager.default.removeItem(atPath: file.path)
@@ -36,7 +40,7 @@ final class AWSCloudWatchLoggingSessionControllerTests: XCTestCase {
3640
/// Then: a flushLogFailure Hub Event is sent to the Logging channel
3741
func testConsumeFailureSendsHubEvent() async throws {
3842
let hubEventExpectation = expectation(description: "Should receive the hub event")
39-
_ = Amplify.Hub.listen(to: .logging) { payload in
43+
unsubscribeToken = Amplify.Hub.listen(to: .logging) { payload in
4044
switch payload.eventName {
4145
case HubPayload.EventName.Logging.flushLogFailure:
4246
hubEventExpectation.fulfill()

Diff for: AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift

+19
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,23 @@ final class LogActorTests: XCTestCase {
9090
}
9191
XCTAssertEqual(decoded.sorted(), entries.sorted())
9292
}
93+
94+
/// Given: a Log file
95+
/// When: LogActor deletes the log
96+
/// Then: the log file is emptied
97+
func testLogActorDeletesEntry() async throws {
98+
let entry = LogEntry(category: "LogActorTests", namespace: nil, level: .error, message: UUID().uuidString, created: .init(timeIntervalSince1970: 0))
99+
try await systemUnderTest.record(entry)
100+
try await systemUnderTest.synchronize()
101+
102+
let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
103+
let fileURL = try XCTUnwrap(files.first)
104+
var contents = try XCTUnwrap(FileManager.default.contents(atPath: fileURL.path))
105+
XCTAssertNotNil(contents)
106+
107+
try await systemUnderTest.deleteLogs()
108+
contents = try XCTUnwrap(FileManager.default.contents(atPath: fileURL.path))
109+
XCTAssertTrue(contents.isEmpty)
110+
}
111+
93112
}

Diff for: AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogRotationTests.swift

+26
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,30 @@ final class LogRotationTests: XCTestCase {
163163
}
164164
}
165165
}
166+
167+
/// Given: a log rotation
168+
/// When: reset is executed
169+
/// Then: log rotation is reset to 0
170+
func testLogRotationResets() throws {
171+
let originalContents = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
172+
XCTAssertEqual(originalContents.map { $0.lastPathComponent }, [
173+
"amplify.0.log"
174+
])
175+
176+
systemUnderTest = try LogRotation(directory: directory,
177+
fileSizeLimitInBytes: fileSizeLimitInBytes)
178+
XCTAssertEqual(systemUnderTest.currentLogFile.fileURL.lastPathComponent, "amplify.1.log")
179+
try systemUnderTest.rotate()
180+
181+
var rotatedContents = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
182+
XCTAssertEqual(rotatedContents.map { $0.lastPathComponent }, [
183+
"amplify.2.log",
184+
"amplify.1.log",
185+
"amplify.0.log",
186+
])
187+
188+
189+
try systemUnderTest.reset()
190+
XCTAssertEqual(systemUnderTest.currentLogFile.fileURL.lastPathComponent, "amplify.0.log")
191+
}
166192
}

0 commit comments

Comments
 (0)