Skip to content

Commit

Permalink
Using OSLog instead of Swift.print
Browse files Browse the repository at this point in the history
  • Loading branch information
goergisn committed Oct 2, 2023
1 parent 47bdc97 commit e3b93fb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
40 changes: 20 additions & 20 deletions AdyenNetworking/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,47 +244,47 @@ public final class APIClient: APIClientProtocol {
}

private func log<R: Request>(urlRequest: URLRequest, request: R) {
adyenPrint("---- Request (/\(request.path)) ----")
Logging.log("---- Request (/\(request.path)) ----")

if let body = urlRequest.httpBody {
adyenPrintAsJSON(body)
Logging.log(body.asJsonString ?? String(describing: body))
}

adyenPrint("---- Request base url (/\(request.path)) ----")
adyenPrint(apiContext.environment.baseURL)
Logging.log("---- Request base url (/\(request.path)) ----")
Logging.log(apiContext.environment.baseURL.absoluteString)

if let headers = urlRequest.allHTTPHeaderFields {
adyenPrint("---- Request Headers (/\(request.path)) ----")
adyenPrintAsJSON(headers)
Logging.log("---- Request Headers (/\(request.path)) ----")
Logging.log(headers.asJsonString ?? String(describing: headers))
}

if let queryParams = urlRequest.url?.queryParameters {
adyenPrint("---- Request query (/\(request.path)) ----")
adyenPrintAsJSON(queryParams)
Logging.log("---- Request query (/\(request.path)) ----")
Logging.log(queryParams.asJsonString ?? String(describing: queryParams))
}

}

private func log<R: Request>(result: URLSessionSuccess, request: R) {
adyenPrint("---- Response Code (/\(request.path)) ----")
adyenPrint(result.statusCode)
Logging.log("---- Response Code (/\(request.path)) ----")
Logging.log("\(result.statusCode)")

adyenPrint("---- Response Headers (/\(request.path)) ----")
adyenPrintAsJSON(result.headers)
Logging.log("---- Response Headers (/\(request.path)) ----")
Logging.log(result.headers.asJsonString ?? String(describing: result.headers))

adyenPrint("---- Response (/\(request.path)) ----")
adyenPrintAsJSON(result.data)
Logging.log("---- Response (/\(request.path)) ----")
Logging.log(result.data.asJsonString ?? String(describing: result.data))
}

private func log<R: Request>(result: URLSessionDownloadSuccess, request: R) {
adyenPrint("---- Response Code (/\(request.path)) ----")
adyenPrint(result.statusCode)
Logging.log("---- Response Code (/\(request.path)) ----")
Logging.log("\(result.statusCode)")

adyenPrint("---- Response Headers (/\(request.path)) ----")
adyenPrintAsJSON(result.headers)
Logging.log("---- Response Headers (/\(request.path)) ----")
Logging.log(result.headers.asJsonString ?? String(describing: result.headers))

adyenPrint("---- Response (/\(request.path)) ----")
adyenPrint(result.url)
Logging.log("---- Response (/\(request.path)) ----")
Logging.log(result.url.absoluteString)
}

/// :nodoc:
Expand Down
70 changes: 35 additions & 35 deletions AdyenNetworking/Helpers/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,59 @@

import func Darwin.fputs
import Foundation
import OSLog

/// Provides control over SDK logging.
public enum Logging {
/// Indicates whether to enable printing to the console.
public static var isEnabled: Bool = false
}

@_spi(AdyenInternal)
public func adyenPrint(_ items: Any..., separator: String = " ", terminator: String = "\n", fileId: String = #fileID) {
guard Logging.isEnabled else { return }
let moduleName = fileId.split(separator: "/").first ?? "AdyenNetworking"
extension Logging {

var items = items
items.insert("[\(moduleName)]", at: 0)
items.insert(ISO8601DateFormatter().string(from: Date()), at: 0)
@available(iOS 14.0, *)
static var logger: Logger { return Logger(subsystem: Bundle.main.bundleIdentifier ?? "com.adyen.Adyen", category: "AdyenNetworking") }

var idx = items.startIndex
let endIdx = items.endIndex

repeat {
Swift.print(items[idx], separator: separator, terminator: idx == (endIdx - 1) ? terminator : separator)
idx += 1
} while idx < endIdx
static func log(_ message: String, as level: OSLogType = .info) {
guard Logging.isEnabled else { return }

if #available(iOS 14.0, *) {
logger.log(level: level, "\(message)")
} else {
print(message)
}
}
}

@_spi(AdyenInternal)
public func adyenPrintAsJSON(_ dictionary: [String: Any]) {
guard Logging.isEnabled else { return }
do {
let jsonData = try JSONSerialization.data(withJSONObject: dictionary, options: jsonOptions())
adyenPrintAsJSON(jsonData)
} catch {
adyenPrint(dictionary)
extension Dictionary where Key == String {

var asJsonString: String? {
guard
let jsonData = try? JSONSerialization.data(withJSONObject: self, options: jsonOptions)
else {
return nil
}

return String(data: jsonData, encoding: .utf8)
}
}

@_spi(AdyenInternal)
public func adyenPrintAsJSON(_ data: Data) {
guard Logging.isEnabled else { return }
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: jsonOptions())
guard let jsonString = String(data: jsonData, encoding: .utf8) else { return }

adyenPrint(jsonString)
} catch {
if let string = String(data: data, encoding: .utf8) {
adyenPrint(string)
extension Data {

var asJsonString: String? {
guard
let jsonObject = try? JSONSerialization.jsonObject(with: self, options: []),
let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: jsonOptions)
else {
return nil
}

return String(data: jsonData, encoding: .utf8)
}
}

private func jsonOptions() -> JSONSerialization.WritingOptions {
private var jsonOptions: JSONSerialization.WritingOptions {

if #available(iOS 13.0, *) {
return [.prettyPrinted, .withoutEscapingSlashes]
} else {
Expand Down

0 comments on commit e3b93fb

Please sign in to comment.