Skip to content

Commit

Permalink
fix(logging): logLevel .none should not produce logs (#3728)
Browse files Browse the repository at this point in the history
* fix(logging): no log should be logged when loglevel is none

* fix cloud watch logging category broken test cases

* resolve more broken test cases
  • Loading branch information
5d authored May 29, 2024
1 parent ce87564 commit ef085ad
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Amplify/Categories/Logging/LogLevel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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,
message())
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

@testable import AWSCloudWatchLoggingPlugin

import Amplify
import XCTest

final class AWSCloudWatchLoggingPluginConfigurationTests: XCTestCase {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@testable import AWSCloudWatchLoggingPlugin

import XCTest
import Amplify

final class AWSCloudWatchLoggingPluginTests: XCTestCase {

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import Amplify
import XCTest

@testable import AWSCloudWatchLoggingPlugin
Expand Down Expand Up @@ -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")
}
Expand Down
39 changes: 39 additions & 0 deletions AmplifyTests/CategoryTests/Logging/DefaultLoggingPluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}

0 comments on commit ef085ad

Please sign in to comment.