From 73db7adc80bafe0e3040f9e589f878b2a8441dcb Mon Sep 17 00:00:00 2001 From: Tuan Pham <103537251+phantumcode@users.noreply.github.com> Date: Fri, 1 Sep 2023 11:48:32 -0500 Subject: [PATCH] fix(logging): create log file if it doesn't exist (#3202) --- .../Persistence/LogActor.swift | 1 + .../Persistence/LogRotation.swift | 6 ++++++ .../LogActorTests.swift | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift b/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift index be5c18c00b..5647af3d38 100644 --- a/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift +++ b/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogActor.swift @@ -29,6 +29,7 @@ actor LogActor { } private func write(_ data: Data) throws { + try rotation.ensureFileExists() if rotation.currentLogFile.hasSpace(for: data) { try rotation.currentLogFile.write(data: data) } else { diff --git a/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogRotation.swift b/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogRotation.swift index 29b6d13f98..6fb4ad7f32 100644 --- a/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogRotation.swift +++ b/AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Persistence/LogRotation.swift @@ -98,6 +98,12 @@ final class LogRotation { index: 0, fileSizeLimitInBytes: fileSizeLimitInBytes) } + + func ensureFileExists() throws { + if !FileManager.default.fileExists(atPath: currentLogFile.fileURL.relativePath) { + try rotate() + } + } /// - Returns: A UInt representing the best guess to which index to use /// next when the number of log files is less that the limit diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift index e4f7cbf863..ebd6db2d24 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LogActorTests.swift @@ -127,4 +127,24 @@ final class LogActorTests: XCTestCase { logs = try await systemUnderTest.getLogs() XCTAssertEqual(logs.count, 2) } + + /// Given: a Log file + /// When: LogActor writes to a log file that doesn't exist + /// Then: the log file is created and the log entry is recorded + func testLogActorCreatesLogFileIfItDoesNotExist() async throws { + let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) + let fileURL = try XCTUnwrap(files.first) + try FileManager.default.removeItem(at: fileURL) + var logs = try await systemUnderTest.getLogs() + XCTAssertEqual(logs.count, 0) + + let entry = LogEntry(category: "LogActorTests", namespace: nil, level: .error, message: UUID().uuidString, created: .init(timeIntervalSince1970: 0)) + try await systemUnderTest.record(entry) + try await systemUnderTest.synchronize() + + logs = try await systemUnderTest.getLogs() + XCTAssertEqual(logs.count, 1) + let contents = try XCTUnwrap(FileManager.default.contents(atPath: fileURL.path)) + XCTAssertNotNil(contents) + } }