diff --git a/Sources/Logging/Log.swift b/Sources/Logging/Log.swift index 4ed28e8..bb20ae9 100644 --- a/Sources/Logging/Log.swift +++ b/Sources/Logging/Log.swift @@ -42,15 +42,15 @@ public enum Log { case warning case error - /// Checks if `self` is above the specified (minimum) log level. A message can be logged if its level is + /// Checks if `self` meets the specified (minimum) log level. A message can be logged if its level is /// *greater than or equal* to another level defined as minimum. /// /// The relationship between levels is as follows: /// `.verbose` < `.debug` < `.info` < `.warning` < `.error` /// /// - Parameter minLevel: The level to compare against `self`. - /// - Returns: `true` if `self` is above the given level, `false` otherwise. - func isAbove(minLevel: Level) -> Bool { minLevel.rawValue <= rawValue } + /// - Returns: `true` if `self` meets the minimum level, `false` otherwise. + func meets(minLevel: Level) -> Bool { rawValue >= minLevel.rawValue } } /// A queue object used to specify `DispatchQueue`'s used in log destinations, ensuring they are serial with the diff --git a/Sources/Logging/Loggers/Log+MultiLogger.swift b/Sources/Logging/Loggers/Log+MultiLogger.swift index 32ddc5b..b0d135b 100644 --- a/Sources/Logging/Loggers/Log+MultiLogger.swift +++ b/Sources/Logging/Loggers/Log+MultiLogger.swift @@ -145,10 +145,10 @@ extension Log { // skip module checks for `nil` modules if let module = module { - guard let moduleMinLevel = modules[module], level.isAbove(minLevel: moduleMinLevel) else { return } + guard let moduleMinLevel = modules[module], level.meets(minLevel: moduleMinLevel) else { return } } - let matchingDestinations = destinations.filter { level.isAbove(minLevel: $0.minLevel) } + let matchingDestinations = destinations.filter { level.meets(minLevel: $0.minLevel) } guard matchingDestinations.isEmpty == false else { return } diff --git a/Sources/Logging/Loggers/Logger.swift b/Sources/Logging/Loggers/Logger.swift index 9535048..99374e0 100644 --- a/Sources/Logging/Loggers/Logger.swift +++ b/Sources/Logging/Loggers/Logger.swift @@ -122,6 +122,8 @@ public extension Logger where Self: LogDestination { function: StaticString ) { + guard level.meets(minLevel: minLevel) else { return } + let item = Log.Item( timestamp: Date(), module: nil, diff --git a/Tests/AlicerceTests/Logging/Loggers/LoggerTestCase.swift b/Tests/AlicerceTests/Logging/Loggers/LoggerTestCase.swift index 8ced820..b2651a8 100644 --- a/Tests/AlicerceTests/Logging/Loggers/LoggerTestCase.swift +++ b/Tests/AlicerceTests/Logging/Loggers/LoggerTestCase.swift @@ -101,6 +101,42 @@ class LoggerTestCase: XCTestCase { log.log(level: .verbose, message: "message", file: "filename.ext", line: 1337, function: "function") } + + func testLog_WithLogDestinationAndLogLevelMeetingMinLevel_ShouldInvokeWrite() { + + let minLevel = Log.Level.verbose + let level = Log.Level.error + + XCTAssert(level.meets(minLevel: minLevel)) + + let log = MockLogDestination() + log.mockMinLevel = minLevel + + log.writeInvokedClosure = { item, _ in + XCTAssertEqual(item.level, level) + XCTAssertEqual(item.message, "message") + XCTAssertEqual(item.file.description, "filename.ext") + XCTAssertEqual(item.line, 1337) + XCTAssertEqual(item.function.description, "function") + } + + log.log(level: level, message: "message", file: "filename.ext", line: 1337, function: "function") + } + + func testLog_WithLogDestinationAndLogLevelNotMeetingMinLevel_ShouldNotInvokeWrite() { + + let minLevel = Log.Level.error + let level = Log.Level.verbose + + XCTAssertFalse(level.meets(minLevel: minLevel)) + + let log = MockLogDestination() + log.mockMinLevel = minLevel + + log.writeInvokedClosure = { _, _ in XCTFail("Unexpected write call") } + + log.log(level: level, message: "message", file: "filename.ext", line: 1337, function: "function") + } } private enum MockModule: String, LogModule {