Removed date formatting from Logger protocol extension

This commit is contained in:
Wojciech Nagrodzki 2018-08-21 19:37:36 +02:00
parent fd7edb0c0d
commit 1803e1dc58
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
5 changed files with 20 additions and 35 deletions

View file

@ -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 `"<file name>:<line> <function>"`.
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
}()

View file

@ -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)
}

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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 ""