Skip to content

Commit

Permalink
Relog meta information for new log files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Majdl committed Sep 19, 2024
1 parent 257f191 commit 7ee1e34
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Sources/Logger/LoggerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class LoggerManager {
// Registered loggers
private let loggers: [Logging]

private let metaInformationBundle: MetaInformationBundle?
private var dateOfLastLog: Date?

private var subscriptions = Set<AnyCancellable>()

/// `LoggerManager` initialization
Expand All @@ -35,6 +38,7 @@ public class LoggerManager {
) {
loggers.forEach { $0.configure() }
self.loggers = loggers
self.metaInformationBundle = metaInformationLoggerBundle

if let applicationCallbackLoggerBundle = applicationCallbackLoggerBundle {
let applicationCallbackLogger = ApplicationCallbackLogger(
Expand All @@ -48,11 +52,6 @@ public class LoggerManager {
}
.store(in: &subscriptions)
}

if let metaInformationLoggerBundle = metaInformationLoggerBundle {
let metaInformation = metaInformationLoggerBundle.types.dictionary(fromBundle: metaInformationLoggerBundle.bundle)
log("Meta information: \(metaInformation)", onLevel: metaInformationLoggerBundle.level)
}
}

/// Log specific entity via the manager.
Expand All @@ -73,14 +72,31 @@ public class LoggerManager {
let log = LogEntry(header: logHeader, location: logLocation, message: message)
let availableLoggers = loggers.availableLoggers(forLevel: log.header.level)

let dateString = DateFormatter.dateFormatter.string(from:)
let currentDate = Date()
let isSameDay = dateString(currentDate) == dateString(dateOfLastLog ?? .distantPast)
dateOfLastLog = currentDate

if !isSameDay {
logMetaInformation()
}

serialQueue.async {
availableLoggers.forEach { $0.log(log) }
}
}


public func logMetaInformation() {
guard let metaInformationBundle else { return }

let metaInformation = metaInformationBundle.types.dictionary(fromBundle: metaInformationBundle.bundle)
log("Meta information: \(metaInformation)", onLevel: metaInformationBundle.level)
}

public func deleteAllLogFiles() {
self.loggers.compactMap { $0 as? FileLogger }
loggers.compactMap { $0 as? FileLogger }
.forEach { $0.deleteAllLogFiles() }
dateOfLastLog = nil
}
}

Expand Down

0 comments on commit 7ee1e34

Please sign in to comment.