mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Removed date formatting from Logger protocol extension
This commit is contained in:
parent
fd7edb0c0d
commit
1803e1dc58
5 changed files with 20 additions and 35 deletions
|
@ -32,18 +32,11 @@ public protocol Logger {
|
||||||
/// Do not call this method directly.
|
/// Do not call this method directly.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - 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.
|
/// - level: The log level.
|
||||||
/// - location: Part of the log message provided by `description(for file: String, line: Int, function: String)` method.
|
/// - 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.
|
/// - object: Part of the log message provided by `description(for object: Any)` method.
|
||||||
func log(time: String, level: LogLevel, location: String, object: String)
|
func log(time: Date, 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
|
|
||||||
|
|
||||||
/// Transforms passed parameters into location textual representation which will be a part of the log message.
|
/// 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.
|
/// - 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) {
|
public func log(_ object: Any, level: LogLevel = .default, file: String = #file, line: Int = #line, function: String = #function) {
|
||||||
let now = Date()
|
let now = Date()
|
||||||
let time = description(for: now)
|
|
||||||
let location = description(for: file, line: line, function: function)
|
let location = description(for: file, line: line, function: function)
|
||||||
let objectDescription = description(for: object)
|
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>"`.
|
/// Returns location in format `"<file name>:<line> <function>"`.
|
||||||
public func description(for file: String, line: Int, function: String) -> String {
|
public func description(for file: String, line: Int, function: String) -> String {
|
||||||
return filename(fromFilePath: file) + ":\(line) \(function)"
|
return filename(fromFilePath: file) + ":\(line) \(function)"
|
||||||
|
@ -123,10 +110,3 @@ extension Logger {
|
||||||
return URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent
|
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
|
|
||||||
}()
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ public final class AgregateLogger: Logger {
|
||||||
self.loggers = loggers
|
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 {
|
for logger in self.loggers {
|
||||||
logger.log(time: time, level: level, location: location, object: object)
|
logger.log(time: time, level: level, location: location, object: object)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,16 @@ import Foundation
|
||||||
|
|
||||||
/// Logger that writes messages into the standard output.
|
/// Logger that writes messages into the standard output.
|
||||||
public final class ConsoleLogger: Logger {
|
public final class ConsoleLogger: Logger {
|
||||||
|
|
||||||
|
private let formatter: DateFormatter
|
||||||
|
|
||||||
public init() {
|
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) {
|
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
||||||
print(time + " <" + level.rawValue + "> " + location + " " + object)
|
print(formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ public final class DiskLogger: Logger {
|
||||||
private let fileURL: URL
|
private let fileURL: URL
|
||||||
private let fileSizeLimit: UInt64
|
private let fileSizeLimit: UInt64
|
||||||
private let rotations: Int
|
private let rotations: Int
|
||||||
|
private let formatter: DateFormatter
|
||||||
private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
private let queue = DispatchQueue(label: "com.wnagrodzki.DiskLogger", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
|
||||||
private var buffer = Data()
|
private var buffer = Data()
|
||||||
private var fileWriter: FileWriter!
|
private var fileWriter: FileWriter!
|
||||||
|
@ -44,10 +45,13 @@ public final class DiskLogger: Logger {
|
||||||
self.fileURL = fileURL
|
self.fileURL = fileURL
|
||||||
self.fileSizeLimit = fileSizeLimit
|
self.fileSizeLimit = fileSizeLimit
|
||||||
self.rotations = rotations
|
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) {
|
public func log(time: Date, level: LogLevel, location: String, object: String) {
|
||||||
let message = time + " <" + level.rawValue + "> " + location + " " + object + "\n"
|
let message = formatter.string(from: time) + " <" + level.rawValue + "> " + location + " " + object + "\n"
|
||||||
log(message)
|
log(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,15 +29,10 @@ public class NullLogger: Logger {
|
||||||
public init() {
|
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
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns empty string to minimize observer effect.
|
|
||||||
public func description(for date: Date) -> String {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns empty string to minimize observer effect.
|
/// Returns empty string to minimize observer effect.
|
||||||
public func description(for file: String, line: Int, function: String) -> String {
|
public func description(for file: String, line: Int, function: String) -> String {
|
||||||
return ""
|
return ""
|
||||||
|
|
Loading…
Add table
Reference in a new issue