Skip to content

Commit

Permalink
add logs in api call
Browse files Browse the repository at this point in the history
  • Loading branch information
patricioxavier8 committed Dec 19, 2024
1 parent 57602ad commit 65df218
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"revision" : "47f9747d12137cd59d783ab376e3ccab7206319b",
"version" : "0.16.1"
}
},
{
"identity" : "xcglogger",
"kind" : "remoteSourceControl",
"location" : "https://github.com/DaveWoodCom/XCGLogger.git",
"state" : {
"revision" : "4def3c1c772ca90ad5e7bfc8ac437c3b0b4276cf",
"version" : "7.1.5"
}
}
],
"version" : 2
Expand Down
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/iosdevzone/IDZSwiftCommonCrypto.git", .upToNextMajor(from: "0.13.1"))
.package(url: "https://github.com/iosdevzone/IDZSwiftCommonCrypto.git", .upToNextMajor(from: "0.13.1")),
.package(url: "https://github.com/DaveWoodCom/XCGLogger.git", .upToNextMajor(from: "7.1.5"))
],

targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "InternxtSwiftCore",
dependencies: [
.product(name: "IDZSwiftCommonCrypto", package: "IDZSwiftCommonCrypto")
.product(name: "IDZSwiftCommonCrypto", package: "IDZSwiftCommonCrypto"),
"XCGLogger"
]),
.testTarget(
name: "InternxtSwiftCoreTests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct APIClient {
if debugResponse == true {
print("API CLIENT ERROR", error)
}
LibraryLogger.shared.logError("API Request Error: \(error.localizedDescription)")
continuation.resume(with: .failure(APIError.failedRequest(error.localizedDescription)))
return
}
Expand All @@ -63,6 +64,11 @@ struct APIClient {
if(debugResponse == true) {
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)")
}

let json = try JSONDecoder().decode(T.self, from: data!)
continuation.resume(with:.success(json))
} catch let DecodingError.dataCorrupted(context) {
Expand Down
52 changes: 52 additions & 0 deletions Sources/InternxtSwiftCore/Utils/LibraryLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import XCGLogger
import Foundation

public class LibraryLogger {
public static let shared = LibraryLogger()
private let logger: 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

logger.add(destination: fileDestination)
}

public func logInfo(_ message: String) {
logger.info(message)
}

public func logDebug(_ message: String) {
logger.debug(message)
}

public func logError(_ message: String) {
logger.error(message)
}

private static func getLogsDirectory() -> URL {
let fileManager = FileManager.default
if let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "JR4S3SY396.group.internxt.desktop") {
let logsDirectory = groupURL.appendingPathComponent("Logs")
try? fileManager.createDirectory(at: logsDirectory, withIntermediateDirectories: true, attributes: nil)
return logsDirectory
}
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let logsDirectory = documentsDirectory.appendingPathComponent("Logs")
try? fileManager.createDirectory(at: logsDirectory, withIntermediateDirectories: true, attributes: nil)
return logsDirectory
}
}

0 comments on commit 65df218

Please sign in to comment.