Skip to content

Commit

Permalink
Add creating symlink to the latest logs folder in LogsRotator
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftyfinch committed Mar 17, 2024
1 parent 723ba36 commit 2e33669
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/RugbyFoundation/Utils/Public/Logger/LogsRotator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import Foundation
/// The service to keep only latest logs.
public final class LogsRotator {
private let logsPath: String
private let shellExecutor: IShellExecutor

private let maxLogs = 10
private var cachedLogsRecord: IFolder?
private let latestSymbolicLinkName = "+latest"

private lazy var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
Expand All @@ -18,8 +20,11 @@ public final class LogsRotator {

/// Initializer.
/// - Parameter logsPath: The path to the logs folder.
public init(logsPath: String) {
/// - Parameter shellExecutor: The service to execute shell commands from Rugby.
public init(logsPath: String,
shellExecutor: IShellExecutor) {
self.logsPath = logsPath
self.shellExecutor = shellExecutor
}

/// Creates the new log file folder per a session and returns it.
Expand All @@ -37,6 +42,7 @@ public final class LogsRotator {

let logsFolderName = dateFormatter.string(from: Date())
let logsRecord = try logsFolder.createFolder(named: logsFolderName)
try createSymbolicLink(at: logsFolder.path, destinationPath: logsRecord.path)
cachedLogsRecord = logsRecord
return logsRecord
}
Expand All @@ -58,4 +64,10 @@ public final class LogsRotator {
return lhsDate.timeIntervalSinceReferenceDate < rhsDate.timeIntervalSinceReferenceDate
}
}

private func createSymbolicLink(at path: String, destinationPath: String) throws {
let latestLogFolderSymlink = "\(path)/\(latestSymbolicLinkName)"
shellExecutor.shell("rm -f \(latestLogFolderSymlink)")
try FileManager.default.createSymbolicLink(atPath: latestLogFolderSymlink, withDestinationPath: destinationPath)
}
}
5 changes: 4 additions & 1 deletion Sources/RugbyFoundation/Vault/Vault.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public final class Vault {
// MARK: - Logs

/// The service to keep only latest logs.
public private(set) lazy var logsRotator = LogsRotator(logsPath: router.logsFolderPath)
public private(set) lazy var logsRotator = LogsRotator(
logsPath: router.logsFolderPath,
shellExecutor: shellExecutor
)

/// The service to log commands metrics.
public private(set) lazy var metricsLogger = MetricsLogger(folderPath: router.logsFolderPath)
Expand Down

0 comments on commit 2e33669

Please sign in to comment.