Skip to content

Commit

Permalink
refactor log system
Browse files Browse the repository at this point in the history
  • Loading branch information
patricioxavier8 committed Dec 20, 2024
1 parent 65df218 commit 6457c1d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct APIClient {
var clientName: String? = nil
var clientVersion: String? = nil
var workspaceHeader: String? = nil

private let logger = LibraryLogger.shared.getLogger(subsystem: "com.internxt.APIClient", category: "Networking")



Expand All @@ -45,7 +45,7 @@ struct APIClient {
if debugResponse == true {
print("API CLIENT ERROR", error)
}
LibraryLogger.shared.logError("API Request Error: \(error.localizedDescription)")
logger.error("API Request Error: \(error.localizedDescription)")
continuation.resume(with: .failure(APIError.failedRequest(error.localizedDescription)))
return
}
Expand All @@ -65,8 +65,9 @@ struct APIClient {
print("\(endpoint.path) response is \(String(decoding: data!, as: UTF8.self))")
}
if !(200...299).contains(httpResponse.statusCode) {

let responseBody = data.flatMap { String(decoding: $0, as: UTF8.self) } ?? "No response body"
LibraryLogger.shared.logError("Error response is :\(responseBody)")
logger.error("Error response is :\(responseBody)")
}

let json = try JSONDecoder().decode(T.self, from: data!)
Expand Down
71 changes: 43 additions & 28 deletions Sources/InternxtSwiftCore/Utils/LibraryLogger.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
import XCGLogger
import Foundation
import XCGLogger

public class LibraryLogger {
public static let shared = LibraryLogger()
private let logger: XCGLogger
private var loggers: [String: XCGLogger] = [:]

private init() {
logger = XCGLogger(identifier: "InternxtSwiftCoreLogger")

logger.setup(level: .debug)


let logsDirectory = LibraryLogger.getLogsDirectory()
let logFile = logsDirectory.appendingPathComponent("InternxtSwiftCore.log")

let fileDestination = FileDestination(writeToFile: logFile.path, identifier: "fileDestination")
fileDestination.outputLevel = .debug
fileDestination.showDate = true
fileDestination.showLogIdentifier = true
fileDestination.showFunctionName = true
fileDestination.showThreadName = true
private init() {}

/// Creates or retrieves a logger for a specific subsystem and category.
public func getLogger(subsystem: String, category: String) -> XCGLogger {
let loggerKey = "\(subsystem).\(category)"

logger.add(destination: fileDestination)
}
if let existingLogger = loggers[loggerKey] {
return existingLogger
}

public func logInfo(_ message: String) {
logger.info(message)
}
let logger = XCGLogger(identifier: loggerKey, includeDefaultDestinations: false)

public func logDebug(_ message: String) {
logger.debug(message)
}
// System log destination (console output)
let systemDestination = AppleSystemLogDestination(identifier: "\(loggerKey).systemDestination")
systemDestination.outputLevel = .debug
systemDestination.showLogIdentifier = false
systemDestination.showFunctionName = false
systemDestination.showThreadName = false
systemDestination.showLevel = true
systemDestination.showFileName = true
systemDestination.showLineNumber = true
systemDestination.showDate = true
logger.add(destination: systemDestination)

public func logError(_ message: String) {
logger.error(message)
// File log destination
if let logsDirectory = LibraryLogger.getLogsDirectory() {
let logFile = logsDirectory.appendingPathComponent("\(loggerKey).log")
let fileDestination = FileDestination(writeToFile: logFile.path, identifier: "\(loggerKey).fileDestination", shouldAppend: true)
fileDestination.outputLevel = .debug
fileDestination.showLogIdentifier = false
fileDestination.showFunctionName = false
fileDestination.showThreadName = false
fileDestination.showLevel = true
fileDestination.showFileName = true
fileDestination.showDate = true
fileDestination.logQueue = DispatchQueue.global(qos: .background)
logger.add(destination: fileDestination)
}

loggers[loggerKey] = logger
return logger
}

private static func getLogsDirectory() -> URL {


/// Retrieves the directory for logs, creating it if necessary.
private static func getLogsDirectory() -> URL? {
let fileManager = FileManager.default
if let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "JR4S3SY396.group.internxt.desktop") {
let logsDirectory = groupURL.appendingPathComponent("Logs")
Expand Down

0 comments on commit 6457c1d

Please sign in to comment.