From 3f359666fb0290e00e480347cf172131f1353d5e Mon Sep 17 00:00:00 2001 From: Di Wu Date: Wed, 29 May 2024 12:13:56 -0700 Subject: [PATCH 1/3] fix(logging): no log should be logged when loglevel is none --- Amplify/Categories/Logging/LogLevel.swift | 2 +- .../Internal/OSLogWrapper.swift | 4 +- .../Logging/DefaultLoggingPluginTests.swift | 39 +++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Amplify/Categories/Logging/LogLevel.swift b/Amplify/Categories/Logging/LogLevel.swift index cee8acfff4..aea36cdfbf 100644 --- a/Amplify/Categories/Logging/LogLevel.swift +++ b/Amplify/Categories/Logging/LogLevel.swift @@ -9,12 +9,12 @@ /// public extension Amplify { enum LogLevel: Int, Codable { + case none case error case warn case info case debug case verbose - case none public init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() diff --git a/Amplify/DefaultPlugins/AWSUnifiedLoggingPlugin/Internal/OSLogWrapper.swift b/Amplify/DefaultPlugins/AWSUnifiedLoggingPlugin/Internal/OSLogWrapper.swift index ad3087d867..66529cc64d 100644 --- a/Amplify/DefaultPlugins/AWSUnifiedLoggingPlugin/Internal/OSLogWrapper.swift +++ b/Amplify/DefaultPlugins/AWSUnifiedLoggingPlugin/Internal/OSLogWrapper.swift @@ -29,7 +29,7 @@ final class OSLogWrapper: Logger { } public func error(_ message: @autoclosure () -> String) { - guard enabled else { return } + guard enabled, logLevel.rawValue >= LogLevel.error.rawValue else { return } os_log("%@", log: osLog, type: OSLogType.error, @@ -37,7 +37,7 @@ final class OSLogWrapper: Logger { } public func error(error: Error) { - guard enabled else { return } + guard enabled, logLevel.rawValue >= LogLevel.error.rawValue else { return } os_log("%@", log: osLog, type: OSLogType.error, diff --git a/AmplifyTests/CategoryTests/Logging/DefaultLoggingPluginTests.swift b/AmplifyTests/CategoryTests/Logging/DefaultLoggingPluginTests.swift index a2b7c47d08..91b9ecf586 100644 --- a/AmplifyTests/CategoryTests/Logging/DefaultLoggingPluginTests.swift +++ b/AmplifyTests/CategoryTests/Logging/DefaultLoggingPluginTests.swift @@ -155,4 +155,43 @@ class DefaultLoggingPluginTests: XCTestCase { globalMessageCorrectlyEvaluated], timeout: 0.1) } + + /// - Given: default configuration + /// - When: + /// - set logLevel to .none + /// - Then: + /// - no log is recorded + func testLoggerWithNoneLogLevel_noLogsAreRecorded() async { + Amplify.Logging.logLevel = .none + + let errorMessageIncorrectlyEvaluated = expectation(description: "error message was incorrectly evaluated") + errorMessageIncorrectlyEvaluated.isInverted = true + Amplify.Logging.error("error \(errorMessageIncorrectlyEvaluated.fulfill())") + + let warnMessageIncorrectlyEvaluated = expectation(description: "warn message was incorrectly evaluated") + warnMessageIncorrectlyEvaluated.isInverted = true + Amplify.Logging.warn("warn \(warnMessageIncorrectlyEvaluated.fulfill())") + + let infoMessageIncorrectlyEvaluated = expectation(description: "info message was incorrectly evaluated") + infoMessageIncorrectlyEvaluated.isInverted = true + Amplify.Logging.info("info \(infoMessageIncorrectlyEvaluated.fulfill())") + + let debugMessageIncorrectlyEvaluated = expectation(description: "debug message was incorrectly evaluated") + debugMessageIncorrectlyEvaluated.isInverted = true + Amplify.Logging.debug("debug \(debugMessageIncorrectlyEvaluated.fulfill())") + + let verboseMessageIncorrectlyEvaluated = expectation(description: "verbose message was incorrectly evaluated") + verboseMessageIncorrectlyEvaluated.isInverted = true + Amplify.Logging.verbose("verbose \(verboseMessageIncorrectlyEvaluated.fulfill())") + await fulfillment( + of: [ + errorMessageIncorrectlyEvaluated, + warnMessageIncorrectlyEvaluated, + infoMessageIncorrectlyEvaluated, + debugMessageIncorrectlyEvaluated, + verboseMessageIncorrectlyEvaluated + ], + timeout: 0.3 + ) + } } From 195c0d018b348384fce1bff548edb15cb5035af1 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Wed, 29 May 2024 12:34:08 -0700 Subject: [PATCH 2/3] fix cloud watch logging category broken test cases --- .../LoggingConstraintsLocalStoreTests.swift | 4 ++-- .../RotatingLogBatchTests.swift | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LoggingConstraintsLocalStoreTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LoggingConstraintsLocalStoreTests.swift index e1f3879389..efb904ad04 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LoggingConstraintsLocalStoreTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/LoggingConstraintsLocalStoreTests.swift @@ -43,7 +43,7 @@ final class LoggingConstraintsLocalStoreTests: XCTestCase { localStore.setLocalLoggingConstraints(loggingConstraints: loggingConstraints) XCTAssertEqual(localStore.getLocalLoggingConstraintsEtag(), "testString") - XCTAssertEqual(localStore.getLocalLoggingConstraints()!.defaultLogLevel.rawValue, 0) + XCTAssertEqual(localStore.getLocalLoggingConstraints()!.defaultLogLevel.rawValue, LogLevel.error.rawValue) XCTAssertTrue(localStore.getLocalLoggingConstraints()!.categoryLogLevel!.isEmpty) XCTAssertTrue(localStore.getLocalLoggingConstraints()!.userLogLevel!.isEmpty) } @@ -60,7 +60,7 @@ final class LoggingConstraintsLocalStoreTests: XCTestCase { localStore.setLocalLoggingConstraints(loggingConstraints: loggingConstraints) XCTAssertEqual(localStore.getLocalLoggingConstraintsEtag(), "testString") - XCTAssertEqual(localStore.getLocalLoggingConstraints()?.defaultLogLevel.rawValue, 1) + XCTAssertEqual(localStore.getLocalLoggingConstraints()?.defaultLogLevel.rawValue, LogLevel.warn.rawValue) XCTAssertEqual(localStore.getLocalLoggingConstraints()!.categoryLogLevel!.count, 1) XCTAssertTrue(localStore.getLocalLoggingConstraints()!.userLogLevel!.isEmpty) } diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/RotatingLogBatchTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/RotatingLogBatchTests.swift index ac8983bd11..39d4e44633 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/RotatingLogBatchTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/RotatingLogBatchTests.swift @@ -6,6 +6,7 @@ // import Foundation +import Amplify import XCTest @testable import AWSCloudWatchLoggingPlugin @@ -43,7 +44,7 @@ final class RotatingLogBatchTests: XCTestCase { let entries = try? rotatingLogBatch.readEntries() XCTAssertEqual(entries?.count, 1) XCTAssertEqual(entries![0].category, "Auth") - XCTAssertEqual(entries![0].logLevel.rawValue, 0) + XCTAssertEqual(entries![0].logLevel.rawValue, LogLevel.error.rawValue) XCTAssertEqual(entries![0].message, "error message") XCTAssertEqual(entries![0].namespace, "namespace") } From b918b5625c779b1365bab639ae663c0993ed52e2 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Wed, 29 May 2024 12:46:04 -0700 Subject: [PATCH 3/3] resolve more broken test cases --- ...AWSCloudWatchLoggingPluginConfigurationTests.swift | 8 ++++---- .../AWSCloudWatchLoggingPluginTests.swift | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginConfigurationTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginConfigurationTests.swift index 2c1abdf61b..4732522eaf 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginConfigurationTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginConfigurationTests.swift @@ -6,7 +6,7 @@ // @testable import AWSCloudWatchLoggingPlugin - +import Amplify import XCTest final class AWSCloudWatchLoggingPluginConfigurationTests: XCTestCase { @@ -36,7 +36,7 @@ final class AWSCloudWatchLoggingPluginConfigurationTests: XCTestCase { XCTAssertEqual(config.awsCloudWatchLoggingPlugin.localStoreMaxSizeInMB, 5) XCTAssertEqual(config.awsCloudWatchLoggingPlugin.logGroupName, "testLogGroup") XCTAssertEqual(config.awsCloudWatchLoggingPlugin.region, "us-east-1") - XCTAssertEqual(config.awsCloudWatchLoggingPlugin.loggingConstraints.defaultLogLevel.rawValue, 0) + XCTAssertEqual(config.awsCloudWatchLoggingPlugin.loggingConstraints.defaultLogLevel.rawValue, LogLevel.error.rawValue) XCTAssertEqual(config.awsCloudWatchLoggingPlugin.loggingConstraints.categoryLogLevel?.count, 2) XCTAssertEqual(config.awsCloudWatchLoggingPlugin.loggingConstraints.userLogLevel?.count, 1) } @@ -56,9 +56,9 @@ final class AWSCloudWatchLoggingPluginConfigurationTests: XCTestCase { XCTFail("Unable to decode from data") return } - XCTAssertEqual(loggingConstraints.defaultLogLevel.rawValue, 0) + XCTAssertEqual(loggingConstraints.defaultLogLevel.rawValue, LogLevel.error.rawValue) XCTAssertEqual(loggingConstraints.categoryLogLevel?.count, 4) XCTAssertEqual(loggingConstraints.userLogLevel?.count, 2) - XCTAssertEqual(loggingConstraints.userLogLevel?["sub1"]?.defaultLogLevel.rawValue, 2) + XCTAssertEqual(loggingConstraints.userLogLevel?["sub1"]?.defaultLogLevel.rawValue, LogLevel.info.rawValue) } } diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginTests.swift index 8262d4b61c..aa06c28514 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginTests/AWSCloudWatchLoggingPluginTests.swift @@ -8,6 +8,7 @@ @testable import AWSCloudWatchLoggingPlugin import XCTest +import Amplify final class AWSCloudWatchLoggingPluginTests: XCTestCase { @@ -33,16 +34,16 @@ final class AWSCloudWatchLoggingPluginTests: XCTestCase { let configuration = AWSCloudWatchLoggingPluginConfiguration(logGroupName: "testLogGroup", region: "us-east-1") let plugin = AWSCloudWatchLoggingPlugin(loggingPluginConfiguration: configuration) var authLogger = plugin.logger(forCategory: "Auth") - XCTAssertEqual(authLogger.logLevel.rawValue, 0) - + XCTAssertEqual(authLogger.logLevel.rawValue, LogLevel.error.rawValue) + authLogger = plugin.logger(forCategory: "Auth", forNamespace: "test") - XCTAssertEqual(authLogger.logLevel.rawValue, 0) + XCTAssertEqual(authLogger.logLevel.rawValue, LogLevel.error.rawValue) let apiLogger = plugin.logger(forCategory: "API", logLevel: .debug) - XCTAssertEqual(apiLogger.logLevel.rawValue, 3) + XCTAssertEqual(apiLogger.logLevel.rawValue, LogLevel.debug.rawValue) let defaultLogger = plugin.logger(forNamespace: "test") - XCTAssertEqual(defaultLogger.logLevel.rawValue, 0) + XCTAssertEqual(defaultLogger.logLevel.rawValue, LogLevel.error.rawValue) } /// Given: a AWSCloudWatchLoggingPlugin