From 1803e1dc584927a3ed196c6c825c9a8dc31f29b2 Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Tue, 21 Aug 2018 19:37:36 +0200 Subject: [PATCH] Removed date formatting from Logger protocol extension --- Logger/Logger.swift | 28 ++++------------------ Logger/Loggers/AgregateLogger.swift | 2 +- Logger/Loggers/ConsoleLogger.swift | 10 ++++++-- Logger/Loggers/DiskLogger/DiskLogger.swift | 8 +++++-- Logger/Loggers/NullLogger.swift | 7 +----- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Logger/Logger.swift b/Logger/Logger.swift index b952d4e..5872896 100644 --- a/Logger/Logger.swift +++ b/Logger/Logger.swift @@ -32,18 +32,11 @@ public protocol Logger { /// Do not call this method directly. /// /// - Parameters: - /// - time: Part of the log message provided by `description(for date: Date)` method. + /// - 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: String, level: LogLevel, location: String, object: String) - - /// Transforms `date` into textual representation which will be a part of the log message. - /// - /// Customization point. Default implementation returns date in format `"yyyy-MM-dd HH:mm:ss.SSS"`. - /// - /// - Parameter date: The date log method was called. - func description(for date: Date) -> String + func log(time: Date, level: LogLevel, location: String, object: String) /// Transforms passed parameters into location textual representation which will be a part of the log message. /// @@ -95,17 +88,11 @@ extension Logger { /// - function: **Do not provide a custom value.** The name of the declaration log method is called within. public func log(_ object: Any, level: LogLevel = .default, file: String = #file, line: Int = #line, function: String = #function) { let now = Date() - let time = description(for: now) let location = description(for: file, line: line, function: function) let objectDescription = description(for: object) - log(time: time, level: level, location: location, object: objectDescription) + log(time: now, level: level, location: location, object: objectDescription) } - - /// Returns date in format `"yyyy-MM-dd HH:mm:ss.SSS"`. - public func description(for date: Date) -> String { - return dateFormatter.string(from: date) - } - + /// Returns location in format `": "`. public func description(for file: String, line: Int, function: String) -> String { return filename(fromFilePath: file) + ":\(line) \(function)" @@ -123,10 +110,3 @@ extension Logger { return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent } } - -private let dateFormatter: DateFormatter = { - let formatter = DateFormatter() - formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" - formatter.timeZone = TimeZone(secondsFromGMT: 0) - return formatter -}() diff --git a/Logger/Loggers/AgregateLogger.swift b/Logger/Loggers/AgregateLogger.swift index d2e1a78..d8cb547 100644 --- a/Logger/Loggers/AgregateLogger.swift +++ b/Logger/Loggers/AgregateLogger.swift @@ -36,7 +36,7 @@ public final class AgregateLogger: Logger { self.loggers = loggers } - public func log(time: String, level: LogLevel, location: String, object: String) { + public func log(time: Date, level: LogLevel, location: String, object: String) { for logger in self.loggers { logger.log(time: time, level: level, location: location, object: object) } diff --git a/Logger/Loggers/ConsoleLogger.swift b/Logger/Loggers/ConsoleLogger.swift index d4eed49..dd96f37 100644 --- a/Logger/Loggers/ConsoleLogger.swift +++ b/Logger/Loggers/ConsoleLogger.swift @@ -26,10 +26,16 @@ import Foundation /// Logger that writes messages into the standard output. public final class ConsoleLogger: Logger { + + private let formatter: DateFormatter + public init() { + formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" + formatter.timeZone = TimeZone(secondsFromGMT: 0) } - public func log(time: String, level: LogLevel, location: String, object: String) { - print(time + " <" + level.rawValue + "> " + location + " " + object) + public func log(time: Date, level: LogLevel, location: String, object: String) { + print(formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object) } } diff --git a/Logger/Loggers/DiskLogger/DiskLogger.swift b/Logger/Loggers/DiskLogger/DiskLogger.swift index b51b9f4..33270b5 100644 --- a/Logger/Loggers/DiskLogger/DiskLogger.swift +++ b/Logger/Loggers/DiskLogger/DiskLogger.swift @@ -30,6 +30,7 @@ public final class DiskLogger: Logger { private let fileURL: URL private let fileSizeLimit: UInt64 private let rotations: Int + private let formatter: DateFormatter private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil) private var buffer = Data() private var fileWriter: FileWriter! @@ -44,10 +45,13 @@ public final class DiskLogger: Logger { self.fileURL = fileURL self.fileSizeLimit = fileSizeLimit self.rotations = rotations + formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" + formatter.timeZone = TimeZone(secondsFromGMT: 0) } - public func log(time: String, level: LogLevel, location: String, object: String) { - let message = time + " <" + level.rawValue + "> " + location + " " + object + "\n" + public func log(time: Date, level: LogLevel, location: String, object: String) { + let message = formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object + "\n" log(message) } diff --git a/Logger/Loggers/NullLogger.swift b/Logger/Loggers/NullLogger.swift index 0787a99..8c8dcb8 100644 --- a/Logger/Loggers/NullLogger.swift +++ b/Logger/Loggers/NullLogger.swift @@ -29,15 +29,10 @@ public class NullLogger: Logger { public init() { } - public func log(time: String, level: LogLevel, location: String, object: String) { + public func log(time: Date, level: LogLevel, location: String, object: String) { // noop } - /// Returns empty string to minimize observer effect. - public func description(for date: Date) -> String { - return "" - } - /// Returns empty string to minimize observer effect. public func description(for file: String, line: Int, function: String) -> String { return ""