Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion Sources/NextcloudKit/Log/NKLogFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public final class NKLogFileManager: @unchecked Sendable {
shared.setConfiguration(logLevel: logLevel)
}

/// Configures filter.
/// - Parameters:
/// - blacklist: Set thing must not be logged.
public static func setBlacklist(blacklist: [String]) {
shared.setBlacklist(blacklist: blacklist)
}

/// Configures whitelist.
/// - Parameters:
/// - whitelist: Set thing must be logged.
public static func setCandidate(whitelist: [String]) {
shared.setWhitelist(whitelist: whitelist)
}

/// Creates the "Logs" folder inside the user's Documents directory if it does not already exist.
///
/// This static method delegates to the singleton instance (`shared`) and ensures
Expand Down Expand Up @@ -98,6 +112,8 @@ public final class NKLogFileManager: @unchecked Sendable {
private let logFileName = "log.txt"
private let logDirectory: URL
public var logLevel: NKLogLevel
private var blacklist: [String] = []
private var whitelist: [String] = []
private var currentLogDate: String
private let logQueue = DispatchQueue(label: "com.nextcloud.LogWriterQueue", attributes: .concurrent)
private let rotationQueue = DispatchQueue(label: "com.nextcloud.LogRotationQueue")
Expand All @@ -109,8 +125,14 @@ public final class NKLogFileManager: @unchecked Sendable {

// MARK: - Initialization

private init(logLevel: NKLogLevel = .normal) {
private init(logLevel: NKLogLevel = .normal, blacklist: [String]? = nil, whitelist: [String]? = nil) {
self.logLevel = logLevel
if let blacklist {
self.blacklist = blacklist
}
if let whitelist {
self.whitelist = whitelist
}

let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let logsFolder = documents.appendingPathComponent("Logs", isDirectory: true)
Expand Down Expand Up @@ -150,6 +172,20 @@ public final class NKLogFileManager: @unchecked Sendable {
self.logLevel = logLevel
}

/// Sets blacklist for the logger.
/// - Parameters:
/// - blacklist:
private func setBlacklist(blacklist: [String]) {
self.blacklist = blacklist
}

/// Sets candidate for the logger.
/// - Parameters:
/// - whitelist:
private func setWhitelist(whitelist: [String]) {
self.whitelist = whitelist
}

// MARK: - Public API

public func writeLog(debug message: String, minimumLogLevel: NKLogLevel = .compact, consoleOnly: Bool = false) {
Expand Down Expand Up @@ -220,9 +256,18 @@ public final class NKLogFileManager: @unchecked Sendable {
let message = message else {
return
}
// Minimum level
if minimumLogLevel > logLevel {
return
}
// Blacklist
if blacklist.contains(where: { message.contains($0) }) {
return
}
// Whitelist
if !whitelist.isEmpty, !whitelist.contains(where: { message.contains($0) }) {
return
}

let fileTimestamp = Self.stableTimestampString()
let consoleTimestamp = Self.localizedTimestampString()
Expand Down
10 changes: 10 additions & 0 deletions Sources/NextcloudKit/NextcloudKit+Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ public extension NextcloudKit {
static func configureLogger(logLevel: NKLogLevel = .normal) {
NKLogFileManager.configure(logLevel: logLevel)
}

/// Configure the shared logger blacklist from NextcloudKit
static func configureLoggerBlacklist(blacklist: [String]) {
NKLogFileManager.setBlacklist(blacklist: blacklist)
}

/// Configure the shared logger whitelist from NextcloudKit
static func configureLoggerWhitelist(whitelist: [String]) {
NKLogFileManager.setCandidate(whitelist: whitelist)
}
}
Loading