diff --git a/Sources/ExampleImplementation/ConfigLogging.swift b/Sources/ExampleImplementation/ConfigLogging.swift index 532edf9..17297a8 100644 --- a/Sources/ExampleImplementation/ConfigLogging.swift +++ b/Sources/ExampleImplementation/ConfigLogging.swift @@ -26,8 +26,8 @@ public final class ConfigLogging { self.config = config } - public func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line _: UInt) { - self.logger.log(level: level, message: message) { text in + public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { + self.logger.log(level: level, message: message, error: error) { text in print(text) } } diff --git a/Sources/ExampleImplementation/FileLogging.swift b/Sources/ExampleImplementation/FileLogging.swift index a6b446a..09c23f7 100644 --- a/Sources/ExampleImplementation/FileLogging.swift +++ b/Sources/ExampleImplementation/FileLogging.swift @@ -32,8 +32,8 @@ public final class FileLogging { self.fileHandler = fileHandler } - public func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line _: UInt) { - self.logger.log(level: level, message: message) { text in + public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { + self.logger.log(level: level, message: message, error: error) { text in self.fileHandler._write(text) } } diff --git a/Sources/ExampleImplementation/SimpleLogger.swift b/Sources/ExampleImplementation/SimpleLogger.swift index 493f999..d088700 100644 --- a/Sources/ExampleImplementation/SimpleLogger.swift +++ b/Sources/ExampleImplementation/SimpleLogger.swift @@ -21,8 +21,8 @@ internal final class SimpleLogger { self.formatter = formatter } - public func log(level: Logging.Level, message: String, printer: (String) -> Void) { - printer("[\(self.label)] \(self.formatter.string(from: Date()))\(self.prettyMetadata.map { " \($0)" } ?? "") \(level): \(message)") + public func log(level: Logging.Level, message: String, error: Error?, printer: (String) -> Void) { + printer("[\(self.label)] \(self.formatter.string(from: Date()))\(self.prettyMetadata.map { " \($0)" } ?? "") \(level): \(message)\(error.map { " \($0)" } ?? "")") } public var logLevel: Logging.Level? { diff --git a/Sources/ExampleImplementation/SimpleLogging.swift b/Sources/ExampleImplementation/SimpleLogging.swift index 62c704a..325aaed 100644 --- a/Sources/ExampleImplementation/SimpleLogging.swift +++ b/Sources/ExampleImplementation/SimpleLogging.swift @@ -24,8 +24,8 @@ public final class SimpleLogging { self.defaultLogLevel = defaultLogLevel } - public func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line _: UInt) { - self.logger.log(level: level, message: message) { text in + public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { + self.logger.log(level: level, message: message, error: error) { text in print(text) } } diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 0ed5dda..ad94779 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -10,7 +10,7 @@ public protocol LogHandler { // `Logger`'s `info`, `error`, or `warning` functions. // // An implementation does not need to check the log level because that has been done before by `Logger` itself. - func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line: UInt) + func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) // This adds metadata to a place the concrete logger considers appropriate. Some loggers // might not support this feature at all. @@ -34,35 +34,35 @@ public struct Logger { } @inlinable - func log(level: Logging.Level, message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + func log(level: Logging.Level, message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { if self.logLevel <= level { - self.handler.log(level: level, message: message(), file: file, function: function, line: line) + self.handler.log(level: level, message: message(), error: error, file: file, function: function, line: line) } } @inlinable - public func trace(_ message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { - self.log(level: .trace, message: message, file: file, function: function, line: line) + public func trace(_ message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.log(level: .trace, message: message, error: error, file: file, function: function, line: line) } @inlinable - public func debug(_ message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { - self.log(level: .debug, message: message, file: file, function: function, line: line) + public func debug(_ message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.log(level: .debug, message: message, error: error, file: file, function: function, line: line) } @inlinable - public func info(_ message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { - self.log(level: .info, message: message, file: file, function: function, line: line) + public func info(_ message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.log(level: .info, message: message, error: error, file: file, function: function, line: line) } @inlinable - public func warning(_ message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { - self.log(level: .warning, message: message, file: file, function: function, line: line) + public func warning(_ message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.log(level: .warning, message: message, error: error, file: file, function: function, line: line) } @inlinable - public func error(_ message: @autoclosure () -> String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { - self.log(level: .error, message: message, file: file, function: function, line: line) + public func error(_ message: @autoclosure () -> String, error: Error? = nil, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) { + self.log(level: .error, message: message, error: error, file: file, function: function, line: line) } @inlinable @@ -121,7 +121,7 @@ extension Logging { case string(String) indirect case dictionary(Metadata) indirect case list([Metadata.Value]) - indirect case lazy(() -> Metadata.Value) + indirect case lazy (() -> Metadata.Value) } public enum Level: Int { @@ -166,7 +166,6 @@ extension Logging.Metadata.Value: Equatable { return false } } - } /// Ships with the logging module, used to multiplex to multiple logging handlers @@ -200,9 +199,9 @@ private class MUXLogHandler: LogHandler { } } - public func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line: UInt) { + public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { self.handlers.forEach { handler in - handler.log(level: level, message: message, file: file, function: function, line: line) + handler.log(level: level, message: message, error: error, file: file, function: function, line: line) } } @@ -239,7 +238,7 @@ private class MUXLogHandler: LogHandler { internal final class StdoutLogger: LogHandler { private let lock = Lock() - public init(label _: String) {} + public init(label: String) {} private var _logLevel: Logging.Level = .info public var logLevel: Logging.Level { @@ -260,9 +259,9 @@ internal final class StdoutLogger: LogHandler { } } - public func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line _: UInt) { + public func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { if level >= self.logLevel { - print("\(self.timestamp()) \(level)\(self.prettyMetadata.map { " \($0)" } ?? "") \(message)") + print("\(self.timestamp()) \(level)\(self.prettyMetadata.map { " \($0)" } ?? "") \(message)\(error.map { " \($0)" } ?? "")") } } @@ -325,13 +324,13 @@ extension Logging.Metadata.Value: CustomStringConvertible { extension Logging.Metadata.Value: ExpressibleByStringInterpolation { #if !swift(>=5.0) - public init(stringInterpolation strings: Logging.Metadata.Value...) { - self = .string(strings.map { $0.description }.reduce("", +)) - } + public init(stringInterpolation strings: Logging.Metadata.Value...) { + self = .string(strings.map { $0.description }.reduce("", +)) + } - public init(stringInterpolationSegment expr: T) { - self = .string(String(stringInterpolationSegment: expr)) - } + public init(stringInterpolationSegment expr: T) { + self = .string(String(stringInterpolationSegment: expr)) + } #endif } diff --git a/Tests/LoggingTests/GlobalLoggingTest.swift b/Tests/LoggingTests/GlobalLoggingTest.swift index 34eb08b..c5323c3 100644 --- a/Tests/LoggingTests/GlobalLoggingTest.swift +++ b/Tests/LoggingTests/GlobalLoggingTest.swift @@ -11,21 +11,21 @@ class GlobalLoggerTest: XCTestCase { // run our program Struct1().doSomething() // test results - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomething") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomething") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomethingElse") - logging.history.assertExist(level: .error, metadata: nil, message: "Struct3::doSomething") - logging.history.assertExist(level: .error, metadata: ["foo": "bar"], message: "Struct3::doSomethingElse") - logging.history.assertExist(level: .warning, metadata: ["foo": "bar"], message: "Struct3::doSomethingElseAsync") - logging.history.assertExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomething") - logging.history.assertExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomethingAsync") - logging.history.assertExist(level: .trace, metadata: ["baz": "qux"], message: "Struct3::doSomethingElse::Local") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct3::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct3::doSomething::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct2::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomething::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomething") + logging.history.assertExist(level: .trace, message: "Struct1::doSomethingElse") + logging.history.assertExist(level: .info, message: "Struct2::doSomething") + logging.history.assertExist(level: .info, message: "Struct2::doSomethingElse") + logging.history.assertExist(level: .error, message: "Struct3::doSomething") + logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .info, message: "TestLibrary::doSomething", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .info, message: "TestLibrary::doSomethingAsync", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::Local", metadata: ["baz": "qux"]) + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::end") + logging.history.assertExist(level: .trace, message: "Struct3::doSomething::end") + logging.history.assertExist(level: .trace, message: "Struct2::doSomethingElse::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomethingElse::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomething::end") } func test2() throws { @@ -37,21 +37,21 @@ class GlobalLoggerTest: XCTestCase { // run our program Struct1().doSomething() // test results - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse") - logging.history.assertNotExist(level: .info, metadata: nil, message: "Struct2::doSomething") - logging.history.assertNotExist(level: .info, metadata: nil, message: "Struct2::doSomethingElse") - logging.history.assertExist(level: .error, metadata: nil, message: "Struct3::doSomething") - logging.history.assertExist(level: .error, metadata: ["foo": "bar"], message: "Struct3::doSomethingElse") - logging.history.assertNotExist(level: .warning, metadata: ["foo": "bar"], message: "Struct3::doSomethingElseAsync") - logging.history.assertNotExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomething") - logging.history.assertNotExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomethingAsync") - logging.history.assertNotExist(level: .trace, metadata: ["baz": "qux"], message: "Struct3::doSomethingElse::Local") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct3::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct3::doSomething::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct2::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse") + logging.history.assertNotExist(level: .info, message: "Struct2::doSomething") + logging.history.assertNotExist(level: .info, message: "Struct2::doSomethingElse") + logging.history.assertExist(level: .error, message: "Struct3::doSomething") + logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["foo": "bar"]) + logging.history.assertNotExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["foo": "bar"]) + logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomething", metadata: ["foo": "bar"]) + logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomethingAsync", metadata: ["foo": "bar"]) + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomethingElse::Local", metadata: ["baz": "qux"]) + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomething::end") + logging.history.assertNotExist(level: .trace, message: "Struct2::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething::end") } func test3() throws { @@ -65,21 +65,21 @@ class GlobalLoggerTest: XCTestCase { // run our program Struct1().doSomething() // test results - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomething") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomethingElse") - logging.history.assertExist(level: .error, metadata: nil, message: "Struct3::doSomething") - logging.history.assertExist(level: .error, metadata: ["foo": "bar"], message: "Struct3::doSomethingElse") - logging.history.assertExist(level: .warning, metadata: ["foo": "bar"], message: "Struct3::doSomethingElseAsync") - logging.history.assertExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomething") - logging.history.assertExist(level: .info, metadata: ["foo": "bar"], message: "TestLibrary::doSomethingAsync") - logging.history.assertNotExist(level: .trace, metadata: ["baz": "qux"], message: "Struct3::doSomethingElse::Local") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct3::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct3::doSomething::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct2::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse::end") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse") + logging.history.assertExist(level: .info, message: "Struct2::doSomething") + logging.history.assertExist(level: .info, message: "Struct2::doSomethingElse") + logging.history.assertExist(level: .error, message: "Struct3::doSomething") + logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .info, message: "TestLibrary::doSomething", metadata: ["foo": "bar"]) + logging.history.assertExist(level: .info, message: "TestLibrary::doSomethingAsync", metadata: ["foo": "bar"]) + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomethingElse::Local", metadata: ["baz": "qux"]) + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct3::doSomething::end") + logging.history.assertNotExist(level: .trace, message: "Struct2::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse::end") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething::end") } } diff --git a/Tests/LoggingTests/LocalLoggingTest.swift b/Tests/LoggingTests/LocalLoggingTest.swift index 7151b88..405eee6 100644 --- a/Tests/LoggingTests/LocalLoggingTest.swift +++ b/Tests/LoggingTests/LocalLoggingTest.swift @@ -12,21 +12,21 @@ class LocalLoggerTest: XCTestCase { let context = Context() Struct1().doSomething(context: context) // test results - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomething") - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomething") - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomethingElse") - logging.history.assertExist(level: .error, metadata: ["bar": "baz"], message: "Struct3::doSomething") - logging.history.assertExist(level: .error, metadata: ["bar": "baz"], message: "Struct3::doSomethingElse") - logging.history.assertExist(level: .warning, metadata: ["bar": "baz"], message: "Struct3::doSomethingElseAsync") - logging.history.assertExist(level: .info, metadata: nil, message: "TestLibrary::doSomething") - logging.history.assertExist(level: .info, metadata: nil, message: "TestLibrary::doSomethingAsync") - logging.history.assertExist(level: .trace, metadata: ["bar": "baz", "baz": "qux"], message: "Struct3::doSomethingElse::Local") - logging.history.assertExist(level: .trace, metadata: ["bar": "baz"], message: "Struct3::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: ["bar": "baz"], message: "Struct3::doSomething::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct2::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse::end") - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct1::doSomething::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomething") + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse") + logging.history.assertExist(level: .info, message: "Struct2::doSomething") + logging.history.assertExist(level: .info, message: "Struct2::doSomethingElse") + logging.history.assertExist(level: .error, message: "Struct3::doSomething", metadata: ["bar": "baz"]) + logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["bar": "baz"]) + logging.history.assertExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["bar": "baz"]) + logging.history.assertExist(level: .info, message: "TestLibrary::doSomething") + logging.history.assertExist(level: .info, message: "TestLibrary::doSomethingAsync") + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::Local", metadata: ["bar": "baz", "baz": "qux"]) + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::end", metadata: ["bar": "baz"]) + logging.history.assertExist(level: .trace, message: "Struct3::doSomething::end", metadata: ["bar": "baz"]) + logging.history.assertExist(level: .trace, message: "Struct2::doSomethingElse::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomethingElse::end") + logging.history.assertExist(level: .trace, message: "Struct1::doSomething::end") } func test2() throws { @@ -39,21 +39,21 @@ class LocalLoggerTest: XCTestCase { let context = Context() Struct1().doSomething(context: context) // test results - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething") // global context - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse") // global context - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomething") // local context - logging.history.assertExist(level: .info, metadata: nil, message: "Struct2::doSomethingElse") // local context - logging.history.assertExist(level: .error, metadata: ["bar": "baz"], message: "Struct3::doSomething") // local context - logging.history.assertExist(level: .error, metadata: ["bar": "baz"], message: "Struct3::doSomethingElse") // local context - logging.history.assertExist(level: .warning, metadata: ["bar": "baz"], message: "Struct3::doSomethingElseAsync") // local context - logging.history.assertNotExist(level: .info, metadata: nil, message: "TestLibrary::doSomething") // global context - logging.history.assertNotExist(level: .info, metadata: nil, message: "TestLibrary::doSomethingAsync") // global context - logging.history.assertExist(level: .trace, metadata: ["bar": "baz", "baz": "qux"], message: "Struct3::doSomethingElse::Local") // hyper local context - logging.history.assertExist(level: .trace, metadata: ["bar": "baz"], message: "Struct3::doSomethingElse::end") // local context - logging.history.assertExist(level: .trace, metadata: ["bar": "baz"], message: "Struct3::doSomething::end") // local context - logging.history.assertExist(level: .trace, metadata: nil, message: "Struct2::doSomethingElse::end") // local context - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomethingElse::end") // global context - logging.history.assertNotExist(level: .trace, metadata: nil, message: "Struct1::doSomething::end") // global context + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething") // global context + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse") // global context + logging.history.assertExist(level: .info, message: "Struct2::doSomething") // local context + logging.history.assertExist(level: .info, message: "Struct2::doSomethingElse") // local context + logging.history.assertExist(level: .error, message: "Struct3::doSomething", metadata: ["bar": "baz"]) // local context + logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["bar": "baz"]) // local context + logging.history.assertExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["bar": "baz"]) // local context + logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomething") // global context + logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomethingAsync") // global context + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::Local", metadata: ["bar": "baz", "baz": "qux"]) // hyper local context + logging.history.assertExist(level: .trace, message: "Struct3::doSomethingElse::end", metadata: ["bar": "baz"]) // local context + logging.history.assertExist(level: .trace, message: "Struct3::doSomething::end", metadata: ["bar": "baz"]) // local context + logging.history.assertExist(level: .trace, message: "Struct2::doSomethingElse::end") // local context + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse::end") // global context + logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething::end") // global context } } diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index c76c262..bc44833 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -2,6 +2,78 @@ import XCTest class LoggingTest: XCTestCase { + func testAutoclosure() throws { + // bootstrap with our test logging impl + let logging = TestLogging() + Logging.bootstrap(logging.make) + var logger = Logging.make("test") + logger.logLevel = .info + logger.trace({ + XCTFail("trace should not be called") + return "trace" + }()) + logger.debug({ + XCTFail("debug should not be called") + return "debug" + }()) + logger.info({ + "info" + }()) + logger.warning({ + "warning" + }()) + logger.error({ + "error" + }()) + XCTAssertEqual(3, logging.history.entries.count, "expected number of entries to match") + logging.history.assertNotExist(level: .trace, message: "trace") + logging.history.assertNotExist(level: .debug, message: "debug") + logging.history.assertExist(level: .info, message: "info") + logging.history.assertExist(level: .warning, message: "warning") + logging.history.assertExist(level: .error, message: "error") + } + + func testAutoclosureWithError() throws { + // bootstrap with our test logging impl + let logging = TestLogging() + Logging.bootstrap(logging.make) + var logger = Logging.make("test") + logger.logLevel = .warning + logger.trace({ + XCTFail("trace should not be called") + return "trace" + }(), error: TestError.boom) + logger.debug({ + XCTFail("debug should not be called") + return "debug" + }(), error: TestError.boom) + logger.info({ + XCTFail("info should not be called") + return "info" + }(), error: TestError.boom) + logger.warning({ + "warning" + }(), error: TestError.boom) + logger.error({ + "error" + }(), error: TestError.boom) + XCTAssertEqual(2, logging.history.entries.count, "expected number of entries to match") + logging.history.assertNotExist(level: .trace, message: "trace", error: TestError.boom) + logging.history.assertNotExist(level: .debug, message: "debug", error: TestError.boom) + logging.history.assertNotExist(level: .info, message: "info", error: TestError.boom) + logging.history.assertExist(level: .warning, message: "warning", error: TestError.boom) + logging.history.assertExist(level: .error, message: "error", error: TestError.boom) + } + + func testWithError() throws { + let logging = TestLogging() + Logging.bootstrap(logging.make) + + let logger = Logging.make("test") + logger.error("oh no!", error: TestError.boom) + logging.history.assertExist(level: .error, message: "oh no!", error: TestError.boom) + } + func testMUX() throws { // bootstrap with our test logging impl let logging1 = TestLogging() @@ -10,13 +82,17 @@ class LoggingTest: XCTestCase { var logger = Logging.make("test") logger.logLevel = .warning - logger.info("hello world") + logger.info("hello world?") logger[metadataKey: "foo"] = "bar" logger.warning("hello world!") - logging1.history.assertNotExist(level: .info, metadata: nil, message: "hello world") - logging2.history.assertNotExist(level: .info, metadata: nil, message: "hello world") - logging1.history.assertExist(level: .warning, metadata: ["foo": "bar"], message: "hello world!") - logging2.history.assertExist(level: .warning, metadata: ["foo": "bar"], message: "hello world!") + logging1.history.assertNotExist(level: .info, message: "hello world?") + logging2.history.assertNotExist(level: .info, message: "hello world?") + logging1.history.assertExist(level: .warning, message: "hello world!", metadata: ["foo": "bar"]) + logging2.history.assertExist(level: .warning, message: "hello world!", metadata: ["foo": "bar"]) + } + + enum TestError: Error { + case boom } func testDictionaryMetadata() { @@ -28,10 +104,10 @@ class LoggingTest: XCTestCase { logger[metadataKey: "nested-dict"] = ["l1key": ["l2key": ["l3key": "l3value"]]] logger.info("hello world!") testLogging.history.assertExist(level: .info, + message: "hello world!", metadata: ["foo": ["bar": "buz"], "empty-dict": [:], - "nested-dict": ["l1key": ["l2key": ["l3key": "l3value"]]]], - message: "hello world!") + "nested-dict": ["l1key": ["l2key": ["l3key": "l3value"]]]]) } func testListMetadata() { @@ -43,10 +119,10 @@ class LoggingTest: XCTestCase { logger[metadataKey: "nested-list"] = ["l1str", ["l2str1", "l2str2"]] logger.info("hello world!") testLogging.history.assertExist(level: .info, + message: "hello world!", metadata: ["foo": ["bar", "buz"], "empty-list": [], - "nested-list": ["l1str", ["l2str1", "l2str2"]]], - message: "hello world!") + "nested-list": ["l1str", ["l2str1", "l2str2"]]]) } func testLazyMetadata() { @@ -54,14 +130,14 @@ class LoggingTest: XCTestCase { Logging.bootstrap(testLogging.make) var logger = Logging.make("\(#function)") logger[metadataKey: "lazy-str"] = .lazy({ "foo" }) - logger[metadataKey: "lazy-list"] = .lazy({ [ .lazy({ "bar" }) ] }) - logger[metadataKey: "lazy-dict"] = .lazy({ [ "buz": .lazy({"qux"})] }) + logger[metadataKey: "lazy-list"] = .lazy({ [.lazy({ "bar" })] }) + logger[metadataKey: "lazy-dict"] = .lazy({ ["buz": .lazy({ "qux" })] }) logger.info("hello world!") testLogging.history.assertExist(level: .info, + message: "hello world!", metadata: ["lazy-str": "foo", "lazy-list": ["bar"], - "lazy-dict": ["buz": "qux"]], - message: "hello world!") + "lazy-dict": ["buz": "qux"]]) } private func dontEvaluateThisString(file: StaticString = #file, line: UInt = #line) -> String { @@ -74,13 +150,12 @@ class LoggingTest: XCTestCase { Logging.bootstrap(testLogging.make) var logger = Logging.make("\(#function)") logger.logLevel = .error - logger[metadataKey: "foo"] = .lazy({"\(self.dontEvaluateThisString())"}) + logger[metadataKey: "foo"] = .lazy({ "\(self.dontEvaluateThisString())" }) - logger.debug(dontEvaluateThisString()) - logger.trace(dontEvaluateThisString()) - logger.info(dontEvaluateThisString()) - logger.warning(dontEvaluateThisString()) - logger.log(level: .warning, message: dontEvaluateThisString()) + logger.debug(self.dontEvaluateThisString()) + logger.trace(self.dontEvaluateThisString()) + logger.info(self.dontEvaluateThisString()) + logger.warning(self.dontEvaluateThisString()) + logger.log(level: .warning, message: self.dontEvaluateThisString()) } - } diff --git a/Tests/LoggingTests/TestLogger.swift b/Tests/LoggingTests/TestLogger.swift index decb1a2..89683ce 100644 --- a/Tests/LoggingTests/TestLogger.swift +++ b/Tests/LoggingTests/TestLogger.swift @@ -30,12 +30,16 @@ internal struct TestLogger: LogHandler { self.logger.logLevel = .trace } - func log(level: Logging.Level, message: String, file: StaticString, function: StaticString, line: UInt) { + func log(level: Logging.Level, message: String, error: Error?, file: StaticString, function: StaticString, line: UInt) { let metadata = self.metadata ?? MDC.global.metadata // use MDC unless set var l = logger // local copy since we gonna override its metadata l.metadata = metadata - l.log(level: level, message: message, file: file, function: function, line: line) - recorder.record(level: level, metadata: metadata, message: message) + if let e = error { + l.log(level: level, message: message, error: e, file: file, function: function, line: line) + } else { + l.log(level: level, message: message, file: file, function: function, line: line) + } + self.recorder.record(level: level, metadata: metadata, message: message, error: error) } private var _logLevel: Logging.Level? @@ -109,9 +113,9 @@ internal class Recorder: History { private let lock = NSLock() private var _entries = [LogEntry]() - func record(level: Logging.Level, metadata: Logging.Metadata?, message: String) { + func record(level: Logging.Level, metadata: Logging.Metadata?, message: String, error: Error?) { return self.lock.withLock { - self._entries.append(LogEntry(level: level, metadata: metadata, message: message)) + self._entries.append(LogEntry(level: level, metadata: metadata, message: message, error: error)) } } @@ -156,22 +160,26 @@ internal struct LogEntry { let level: Logging.Level let metadata: Logging.Metadata? let message: String + let error: Error? } extension History { - func assertExist(level: Logging.Level, metadata: Logging.Metadata?, message: String, file: StaticString = #file, line: UInt = #line) { - let entry = self.find(level: level, metadata: metadata, message: message) - XCTAssertNotNil(entry, "entry not found: \(level), \(String(describing: metadata)), \(message)", file: file, line: line) + func assertExist(level: Logging.Level, message: String, metadata: Logging.Metadata? = nil, error: Error? = nil, file: StaticString = #file, line: UInt = #line) { + let entry = self.find(level: level, message: message, metadata: metadata, error: error) + XCTAssertNotNil(entry, "entry not found: \(level), \(String(describing: metadata)), \(message) \(String(describing: error))", file: file, line: line) } - func assertNotExist(level: Logging.Level, metadata: Logging.Metadata?, message: String, file: StaticString = #file, line: UInt = #line) { - let entry = self.find(level: level, metadata: metadata, message: message) - XCTAssertNil(entry, "entry was found: \(level), \(String(describing: metadata)), \(message)]", file: file, line: line) + func assertNotExist(level: Logging.Level, message: String, metadata: Logging.Metadata? = nil, error: Error? = nil, file: StaticString = #file, line: UInt = #line) { + let entry = self.find(level: level, message: message, metadata: metadata, error: error) + XCTAssertNil(entry, "entry was found: \(level), \(String(describing: metadata)), \(message) \(String(describing: error))", file: file, line: line) } - func find(level: Logging.Level, metadata: Logging.Metadata?, message: String) -> LogEntry? { + func find(level: Logging.Level, message: String, metadata: Logging.Metadata? = nil, error: Error?) -> LogEntry? { return self.entries.first { entry in - entry.level == level && entry.message == message && entry.metadata ?? [:] == metadata ?? [:] + entry.level == level && + entry.message == message && + entry.metadata ?? [:] == metadata ?? [:] && + entry.error?.localizedDescription ?? "" == error?.localizedDescription ?? "" } } }