Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ final class AWSCloudWatchLoggingSession {
let directory = try directory(for: category, userIdentifier: userIdentifier)
try fileManager.createDirectory(at: directory, withIntermediateDirectories: true)
try (directory as NSURL).setResourceValue(true, forKey: URLResourceKey.isExcludedFromBackupKey)
let cacheMaxSizeInBytes = localStoreMaxSizeInMB * 1048576

// Calculate appropriate file size limit (individual file, not total cache)
// Use a reasonable file size limit that's a fraction of the total cache size
// Ensure it meets the minimum requirement of 1KB
let totalCacheSizeInBytes = localStoreMaxSizeInMB * 1048576
let fileSizeLimitInBytes = max(
LogRotation.minimumFileSizeLimitInBytes,
totalCacheSizeInBytes / LogRotation.fileCountLimit
)

return try RotatingLogger(directory: directory,
category: category,
namespace: namespace,
logLevel: logLevel,
fileSizeLimitInBytes: cacheMaxSizeInBytes)
fileSizeLimitInBytes: fileSizeLimitInBytes)
}

private static func directory(for category: String, userIdentifier: String?, fileManager: FileManager = .default) throws -> URL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ struct CloudWatchLoggingStreamNameFormatter {
func formattedStreamName() async -> String {
return "\(await deviceIdentifier ?? "").\(userIdentifier ?? "guest")"
}

// Add the missing deviceIdentifierFromBundle static method
private static func deviceIdentifierFromBundle() -> String? {
// Use bundle identifier as a fallback device identifier
// This provides a consistent identifier per app installation
return Bundle.main.bundleIdentifier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ final class LogRotation {
}

static let minimumFileSizeLimitInBytes = 1024 /* 1KB */
static let fileCountLimit: Int = 5

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

let directory: URL
let fileCountLimit: Int = 5

let fileSizeLimitInBytes: UInt64

private(set) var currentLogFile: LogFile {
Expand All @@ -40,7 +41,7 @@ final class LogRotation {
self.directory = directory.absoluteURL
self.fileSizeLimitInBytes = UInt64(fileSizeLimitInBytes)
self.currentLogFile = try Self.selectNextLogFile(from: self.directory,
fileCountLimit: fileCountLimit,
fileCountLimit: Self.fileCountLimit,
fileSizeLimitInBytes: self.fileSizeLimitInBytes)
}

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

Expand Down
Loading