mirror of
https://github.com/wnagrodzki/SwiftLogger.git
synced 2025-04-07 04:51:52 +02:00
Uncoupled DiskLogger from FileManager
This commit is contained in:
parent
11cdd72a04
commit
7c58a50778
2 changed files with 13 additions and 6 deletions
|
@ -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 fileSystem: FileSystem
|
||||||
private let formatter: DateFormatter
|
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()
|
||||||
|
@ -41,10 +42,11 @@ public final class DiskLogger: Logger {
|
||||||
/// - fileURL: URL of the log file.
|
/// - fileURL: URL of the log file.
|
||||||
/// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation.
|
/// - fileSizeLimit: Maximum size log file can reach in bytes. Attempt to exceeding that limit triggers log files rotation.
|
||||||
/// - rotations: Number of times log files are rotated before being removed.
|
/// - rotations: Number of times log files are rotated before being removed.
|
||||||
public init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int) {
|
public init(fileURL: URL, fileSizeLimit: UInt64, rotations: Int, fileSystem: FileSystem) {
|
||||||
self.fileURL = fileURL
|
self.fileURL = fileURL
|
||||||
self.fileSizeLimit = fileSizeLimit
|
self.fileSizeLimit = fileSizeLimit
|
||||||
self.rotations = rotations
|
self.rotations = rotations
|
||||||
|
self.fileSystem = fileSystem
|
||||||
formatter = DateFormatter()
|
formatter = DateFormatter()
|
||||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
|
||||||
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
formatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||||
|
@ -80,8 +82,8 @@ public final class DiskLogger: Logger {
|
||||||
|
|
||||||
private func openFileWriter() throws {
|
private func openFileWriter() throws {
|
||||||
guard fileWriter == nil else { return }
|
guard fileWriter == nil else { return }
|
||||||
if FileManager.default.fileExists(atPath: fileURL.path) == false {
|
if fileSystem.itemExists(at: fileURL) == false {
|
||||||
FileManager.default.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
|
_ = fileSystem.createFile(at: fileURL)
|
||||||
}
|
}
|
||||||
fileWriter = try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
fileWriter = try FileWriter(fileURL: fileURL, fileSizeLimit: fileSizeLimit)
|
||||||
}
|
}
|
||||||
|
@ -97,7 +99,7 @@ public final class DiskLogger: Logger {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func rotateLogFiles() throws {
|
private func rotateLogFiles() throws {
|
||||||
let logrotate = Logrotate(fileURL: fileURL, rotations: rotations, fileSystem: FileManager.default)
|
let logrotate = Logrotate(fileURL: fileURL, rotations: rotations, fileSystem: fileSystem)
|
||||||
try logrotate.rotate()
|
try logrotate.rotate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,15 +24,20 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol FileSystem {
|
public protocol FileSystem {
|
||||||
func itemExists(at URL: URL) -> Bool
|
func itemExists(at URL: URL) -> Bool
|
||||||
func removeItem(at URL: URL) throws
|
func removeItem(at URL: URL) throws
|
||||||
func moveItem(at srcURL: URL, to dstURL: URL) throws
|
func moveItem(at srcURL: URL, to dstURL: URL) throws
|
||||||
|
func createFile(at URL: URL) -> Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
extension FileManager: FileSystem {
|
extension FileManager: FileSystem {
|
||||||
|
|
||||||
func itemExists(at URL: URL) -> Bool {
|
public func itemExists(at URL: URL) -> Bool {
|
||||||
return fileExists(atPath: URL.path)
|
return fileExists(atPath: URL.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func createFile(at URL: URL) -> Bool {
|
||||||
|
return createFile(atPath: URL.path, contents: nil, attributes: nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue