Skip to content

Commit

Permalink
=multiplex #139 more properly handle logLevel in multiplex handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso committed Jun 29, 2020
1 parent afc50b2 commit e26592e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ extension Logger {
/// well as the `metadata` for this `LogHandler`. Any subsequent `LogHandler`s used to initialise a
/// `MultiplexLogHandler` are merely to emit the log message to another place.
public struct MultiplexLogHandler: LogHandler {

private var effectiveLogLevel: Logger.Level
private var handlers: [LogHandler]

/// Create a `MultiplexLogHandler`.
Expand All @@ -472,24 +474,30 @@ public struct MultiplexLogHandler: LogHandler {
public init(_ handlers: [LogHandler]) {
assert(!handlers.isEmpty)
self.handlers = handlers
self.effectiveLogLevel = MultiplexLogHandler.effectiveLogLevel(handlers.map { $0.logLevel })
}

public var logLevel: Logger.Level {
get {
return self.handlers[0].logLevel
return self.effectiveLogLevel
}
set {
self.mutatingForEachHandler {
$0.logLevel = newValue
}
self.effectiveLogLevel = MultiplexLogHandler.effectiveLogLevel(self.handlers.map { $0.logLevel })
}
}

static func effectiveLogLevel(_ levels: [Logger.Level]) -> Logger.Level {
levels.min() ?? .trace
}

public func log(level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
file: String, function: String, line: UInt) {
self.handlers.forEach { handler in
for handler in self.handlers where handler.logLevel <= level {
handler.log(level: level, message: message, metadata: metadata, file: file, function: function, line: line)
}
}
Expand Down
68 changes: 68 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,74 @@ class LoggingTest: XCTestCase {
logging2.history.assertExist(level: .warning, message: "hello world!", metadata: ["foo": "bar"])
}

func testMultiplexLogHandlerWithVariousLogLevels() throws {
let logging1 = TestLogging()
let logging2 = TestLogging()

var logger1 = logging1.make(label: "1")
logger1.logLevel = .info

var logger2 = logging2.make(label: "2")
logger2.logLevel = .debug

LoggingSystem.bootstrapInternal { _ in
return MultiplexLogHandler([logger1, logger2])
}

let multiplexLogger = Logger(label: "test")
multiplexLogger.trace("trace")
multiplexLogger.debug("debug")
multiplexLogger.info("info")
multiplexLogger.warning("warning")

logging1.history.assertNotExist(level: .trace, message: "trace")
logging1.history.assertNotExist(level: .debug, message: "debug")
logging1.history.assertExist(level: .info, message: "info")
logging1.history.assertExist(level: .warning, message: "warning")

logging2.history.assertNotExist(level: .trace, message: "trace")
logging2.history.assertExist(level: .debug, message: "debug")
logging2.history.assertExist(level: .info, message: "info")
logging2.history.assertExist(level: .warning, message: "warning")
}

func testMultiplexLogHandlerNeedNotMaterializeValuesMultipleTimes() throws {
let logging1 = TestLogging()
let logging2 = TestLogging()

var logger1 = logging1.make(label: "1")
logger1.logLevel = .info

var logger2 = logging2.make(label: "2")
logger2.logLevel = .info

LoggingSystem.bootstrapInternal { _ in
return MultiplexLogHandler([logger1, logger2])
}

var messageMaterializations: Int = 0
var metadataMaterializations: Int = 0

let multiplexLogger = Logger(label: "test")
multiplexLogger.info(
{ () -> Logger.Message in
messageMaterializations += 1
return "info"
}(),
metadata: { () ->
Logger.Metadata in metadataMaterializations += 1
return [:]
}()
)

logging1.history.assertExist(level: .info, message: "info")
logging2.history.assertExist(level: .info, message: "info")

XCTAssertEqual(messageMaterializations, 1)
XCTAssertEqual(metadataMaterializations, 1)
}


enum TestError: Error {
case boom
}
Expand Down

0 comments on commit e26592e

Please sign in to comment.