Skip to content

Commit 34bc774

Browse files
authored
fix(logging): crash on release builds in rotation logger (#4009)
* fix(logging): crash on release builds in rotation logger * worked on review comment
1 parent 3551df8 commit 34bc774

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/AWSCloudWatchLoggingSession.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,21 @@ final class AWSCloudWatchLoggingSession {
4343
let directory = try directory(for: category, userIdentifier: userIdentifier)
4444
try fileManager.createDirectory(at: directory, withIntermediateDirectories: true)
4545
try (directory as NSURL).setResourceValue(true, forKey: URLResourceKey.isExcludedFromBackupKey)
46-
let cacheMaxSizeInBytes = localStoreMaxSizeInMB * 1048576
46+
47+
// Calculate appropriate file size limit (individual file, not total cache)
48+
// Use a reasonable file size limit that's a fraction of the total cache size
49+
// Ensure it meets the minimum requirement of 1KB
50+
let totalCacheSizeInBytes = localStoreMaxSizeInMB * 1048576
51+
let fileSizeLimitInBytes = max(
52+
LogRotation.minimumFileSizeLimitInBytes,
53+
totalCacheSizeInBytes / LogRotation.fileCountLimit
54+
)
55+
4756
return try RotatingLogger(directory: directory,
4857
category: category,
4958
namespace: namespace,
5059
logLevel: logLevel,
51-
fileSizeLimitInBytes: cacheMaxSizeInBytes)
60+
fileSizeLimitInBytes: fileSizeLimitInBytes)
5261
}
5362

5463
private static func directory(for category: String, userIdentifier: String?, fileManager: FileManager = .default) throws -> URL {

AmplifyPlugins/Logging/Sources/AWSCloudWatchLoggingPlugin/Consumer/CloudWatchLoggingStreamNameFormatter.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ struct CloudWatchLoggingStreamNameFormatter {
4444
func formattedStreamName() async -> String {
4545
return "\(await deviceIdentifier ?? "").\(userIdentifier ?? "guest")"
4646
}
47+
48+
// Add the missing deviceIdentifierFromBundle static method
49+
private static func deviceIdentifierFromBundle() -> String? {
50+
// Use bundle identifier as a fallback device identifier
51+
// This provides a consistent identifier per app installation
52+
return Bundle.main.bundleIdentifier
53+
}
4754
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ final class LogRotation {
1717
}
1818

1919
static let minimumFileSizeLimitInBytes = 1024 /* 1KB */
20+
static let fileCountLimit: Int = 5
2021

2122
/// The name pattern of files managed by `LogRotation`.
2223
private static let filePattern = #"amplify[.]([0-9])[.]log"#
2324

2425
let directory: URL
25-
let fileCountLimit: Int = 5
26+
2627
let fileSizeLimitInBytes: UInt64
2728

2829
private(set) var currentLogFile: LogFile {
@@ -40,7 +41,7 @@ final class LogRotation {
4041
self.directory = directory.absoluteURL
4142
self.fileSizeLimitInBytes = UInt64(fileSizeLimitInBytes)
4243
self.currentLogFile = try Self.selectNextLogFile(from: self.directory,
43-
fileCountLimit: fileCountLimit,
44+
fileCountLimit: Self.fileCountLimit,
4445
fileSizeLimitInBytes: self.fileSizeLimitInBytes)
4546
}
4647

@@ -55,7 +56,7 @@ final class LogRotation {
5556
/// 3. If no files matching #1 are present, the file with the oldest last modified date is cleared and selected.
5657
func rotate() throws {
5758
self.currentLogFile = try Self.selectNextLogFile(from: self.directory,
58-
fileCountLimit: self.fileCountLimit,
59+
fileCountLimit: Self.fileCountLimit,
5960
fileSizeLimitInBytes: self.fileSizeLimitInBytes)
6061
}
6162

0 commit comments

Comments
 (0)