diff --git a/Logger/Logger.swift b/Logger/Logger.swift index 2f76349..7953f48 100644 --- a/Logger/Logger.swift +++ b/Logger/Logger.swift @@ -35,8 +35,8 @@ public protocol Logger { /// - time: The date log method was called. /// - level: The log level. /// - location: Part of the log message provided by `description(for file: String, line: Int, function: String)` method. - /// - object: Part of the log message provided by `description(for object: Any)` method. - func log(time: Date, level: LogLevel, location: String, object: String) + /// - message: The message to be logged. + func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) /// Transforms passed parameters into location textual representation which will be a part of the log message. /// @@ -47,14 +47,6 @@ public protocol Logger { /// - line: The line number log method was called at. /// - function: The name of the declaration log method was called within. func description(for file: String, line: Int, function: String) -> String - - /// Transforms `object` into textual representation which will be a part of the log message. - /// - /// Customization point. Default implementation returns `logDescription` for objects implementing `LogStringConvertible`, - /// or `String(describing:object)` otherwise. - /// - /// - Parameter object: The object to be logged. - func description(for object: Any) -> String } /// Log level controls the conditions under which a message should be logged. @@ -107,16 +99,15 @@ extension Logger { /// Sends object to the logging system, optionally specifying a custom log level. /// /// - Parameters: - /// - object: The object to be logged. + /// - message: The message to be logged. /// - level: The log level. /// - file: **Do not provide a custom value.** The path to the file log method is called from. /// - line: **Do not provide a custom value.** The line number log method is called at. /// - function: **Do not provide a custom value.** The name of the declaration log method is called within. - public func log(_ object: Any, level: LogLevel, file: String = #file, line: Int = #line, function: String = #function) { + public func log(_ message: @autoclosure () -> String, level: LogLevel, file: String = #file, line: Int = #line, function: String = #function) { let now = Date() let location = description(for: file, line: line, function: function) - let objectDescription = description(for: object) - log(time: now, level: level, location: location, object: objectDescription) + log(time: now, level: level, location: location, message: message) } /// Returns location in format `": "`. @@ -124,14 +115,6 @@ extension Logger { return filename(fromFilePath: file) + ":\(line) \(function)" } - /// Returns `logDescription` for objects implementing `LogStringConvertible`, or `String(describing:object)` otherwise. - public func description(for object: Any) -> String { - if let logStringConvertible = object as? LogStringConvertible { - return logStringConvertible.logDescription - } - return String(describing: object) - } - private func filename(fromFilePath path: String) -> String { return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent } diff --git a/Logger/Loggers/AgregateLogger.swift b/Logger/Loggers/AgregateLogger.swift index d8cb547..d9bffe4 100644 --- a/Logger/Loggers/AgregateLogger.swift +++ b/Logger/Loggers/AgregateLogger.swift @@ -36,9 +36,9 @@ public final class AgregateLogger: Logger { self.loggers = loggers } - public func log(time: Date, level: LogLevel, location: String, object: String) { + public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) { for logger in self.loggers { - logger.log(time: time, level: level, location: location, object: object) + logger.log(time: time, level: level, location: location, message: message) } } } diff --git a/Logger/Loggers/ConsoleLogger.swift b/Logger/Loggers/ConsoleLogger.swift index a103bd8..ef8696a 100644 --- a/Logger/Loggers/ConsoleLogger.swift +++ b/Logger/Loggers/ConsoleLogger.swift @@ -35,7 +35,7 @@ public final class ConsoleLogger: Logger { formatter.timeZone = TimeZone(secondsFromGMT: 0) } - public func log(time: Date, level: LogLevel, location: String, object: String) { - print(formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object) + public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) { + print(formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + message()) } } diff --git a/Logger/Loggers/DiskLogger/DiskLogger.swift b/Logger/Loggers/DiskLogger/DiskLogger.swift index f4b1005..f06a404 100644 --- a/Logger/Loggers/DiskLogger/DiskLogger.swift +++ b/Logger/Loggers/DiskLogger/DiskLogger.swift @@ -50,8 +50,8 @@ public final class DiskLogger: Logger { formatter.timeZone = TimeZone(secondsFromGMT: 0) } - public func log(time: Date, level: LogLevel, location: String, object: String) { - let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + object + "\n" + public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) { + let message = formatter.string(from: time) + " <" + level.logDescription + "> " + location + " " + message() + "\n" log(message) } diff --git a/Logger/Loggers/NullLogger.swift b/Logger/Loggers/NullLogger.swift index 8c8dcb8..64adf30 100644 --- a/Logger/Loggers/NullLogger.swift +++ b/Logger/Loggers/NullLogger.swift @@ -29,7 +29,7 @@ public class NullLogger: Logger { public init() { } - public func log(time: Date, level: LogLevel, location: String, object: String) { + public func log(time: Date, level: LogLevel, location: String, message: @autoclosure () -> String) { // noop } @@ -37,9 +37,4 @@ public class NullLogger: Logger { public func description(for file: String, line: Int, function: String) -> String { return "" } - - /// Returns empty string to minimize observer effect. - public func description(for object: Any) -> String { - return "" - } }