Skip to content

Commit

Permalink
Migrate to Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
marekpridal committed Feb 9, 2025
1 parent 833c177 commit b357c36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 69 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PackageDescription
//swiftlint:disable all
let package = Package(
name: "LogKit",
platforms: [.macOS(.v11), .iOS(.v14), .macCatalyst(.v14), .tvOS(.v14), .watchOS(.v7), .visionOS(.v1)],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
Expand Down
99 changes: 30 additions & 69 deletions Sources/LogKit/LogKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,31 @@ public enum Log {
#endif
public static func function(_ function: String, in file: String) {
guard enabledLogging.contains(.function) else { return }
#if canImport(Darwin)
let logger = Logger(subsystem: subsystem, category: Category.function.rawValue)
logger.debug("\(function) \(file)")
#elseif os(Android)
#endif
}

public static func `default`(_ string: String) {
guard enabledLogging.contains(.default) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.default.rawValue)
os_log("%{PRIVATE}@", log: logObject, type: .debug, string)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.default.rawValue)
logger.debug("\(string)")
}

public static func requestCalled(function: String) {
guard enabledLogging.contains(.networking) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.networking.rawValue)
os_log("%{PRIVATE}@ already called", log: logObject, type: .debug, function)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.networking.rawValue)
logger.debug("\(function) already called")
}

public static func expiration(date: Date) {
guard enabledLogging.contains(.expiration) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.expiration.rawValue)
os_log("[GMT] Valid until %{PRIVATE}@", log: logObject, type: .debug, date.debugDescription)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.expiration.rawValue)
logger.debug("[GMT] Valid until \(date.debugDescription)")
}

public static func request(_ request: URLRequest) {
guard enabledLogging.contains(.networking) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.networking.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.networking.rawValue)
var message = "\n---REQUEST------------------\n"
message.append("URL -> \((request.url?.absoluteString ?? ""))\n")
message.append("METHOD -> \(request.httpMethod ?? "")\n")
Expand All @@ -93,15 +80,12 @@ public enum Log {
message.append("BODY -> \(String(data: body, encoding: .utf8) ?? "")\n")
}
message.append("----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, message)
#elseif os(Android)
#endif
logger.debug("\(message)")
}

public static func response(_ response: URLResponse?, data: Data?) {
guard enabledLogging.contains(.networking) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.networking.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.networking.rawValue)
guard let response = response, let data = data else { return }
var message = "\n---RESPONSE------------------\n"
message.append("URL -> \(response.url?.absoluteString ?? "")\n")
Expand All @@ -114,107 +98,84 @@ public enum Log {
message.append("}\n")
message.append("Response data -> \(String(data: data, encoding: .utf8) ?? "")\n")
message.append("----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, message)
#elseif os(Android)
#endif
logger.debug("\(message)")
}

public static func function(_ function: String, text: String) {
guard enabledLogging.contains(.function) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.function.rawValue)
os_log("%{PRIVATE}@ %{PRIVATE}@", log: logObject, type: .debug, function, text)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.function.rawValue)
logger.debug("\(function) \(text)")
}

public static func inAppPurchase(_ string: String) {
guard enabledLogging.contains(.inAppPurchase) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
os_log("%{PRIVATE}@", log: logObject, type: .debug, string)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
logger.debug("\(string)")
}

public static func error(_ error: Error) {
guard enabledLogging.contains(.error) else { return }
#if canImport(Darwin)
let logObject = OSLog(subsystem: subsystem, category: Category.error.rawValue)
os_log("%{PRIVATE}@", log: logObject, type: .error, error.localizedDescription)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.error.rawValue)
logger.debug("\(error.localizedDescription)")
}

public static func database(_ string: String) {
guard enabledLogging.contains(.database) else { return }
#if canImport(Darwin)
let log = OSLog(subsystem: subsystem, category: LogKit.Log.Category.database.rawValue)
os_log("%{PRIVATE}@", log: log, type: .debug, string)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.database.rawValue)
logger.debug("\(string)")
}

public static func dependencyInjection(_ string: String) {
guard enabledLogging.contains(.dependencyInjection) else { return }
#if canImport(Darwin)
let log = OSLog(subsystem: subsystem, category: LogKit.Log.Category.dependencyInjection.rawValue)
os_log("%{PRIVATE}@", log: log, type: .debug, string)
#elseif os(Android)
#endif
let logger = Logger(subsystem: subsystem, category: Category.dependencyInjection.rawValue)
logger.debug("\(string)")
}
#if canImport(Darwin)
@available(watchOS 6.2, *)
public static func products(request: SKProductsRequest) {
guard enabledLogging.contains(.inAppPurchase) else { return }
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
var requestMessage = "\n---REQUEST------------------\n"
requestMessage.append("\(request)")
requestMessage.append("\n----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, requestMessage)
logger.debug("\(requestMessage)")
}

@available(watchOS 6.2, *)
public static func products(response: SKProductsResponse) {
guard enabledLogging.contains(.inAppPurchase) else { return }
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
var responseMessage = "\n---RESPONSE------------------\n"
responseMessage.append("Invalid product identifiers \(response.invalidProductIdentifiers)")
responseMessage.append("\n----------------------------\n")
responseMessage.append("Products \(response.products)")
responseMessage.append("\n----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, responseMessage)
logger.debug("\(responseMessage)")
}

@available(watchOS 6.2, *)
public static func products(request: SKRequest) {
guard enabledLogging.contains(.inAppPurchase) else { return }
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
var requestMessage = "\n---REQUEST------------------\n"
requestMessage.append("\(request)")
requestMessage.append("\n----------------------------\n")
Log.default(requestMessage)
os_log("%{PRIVATE}@", log: logObject, type: .debug, requestMessage)
logger.debug("\(requestMessage)")
}

@available(watchOS 6.2, *)
public static func paymentQueue(_ queue: SKPaymentQueue) {
guard enabledLogging.contains(.inAppPurchase) else { return }
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
var requestMessage = "\n---QUEUE------------------\n"
requestMessage.append("\(queue)")
requestMessage.append("\n----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, requestMessage)
logger.debug("\(requestMessage)")
}

@available(watchOS 6.2, *)
public static func payment(transactions: [SKPaymentTransaction]) {
guard enabledLogging.contains(.inAppPurchase) else { return }
let logObject = OSLog(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
let logger = Logger(subsystem: subsystem, category: Category.inAppPurchase.rawValue)
var responseMessage = "\n---UPDATED TRANSACTIONS------------------\n"
responseMessage.append("\(transactions)")
responseMessage.append("\n----------------------------\n")
os_log("%{PRIVATE}@", log: logObject, type: .debug, responseMessage)
logger.debug("\(responseMessage)")
}
#endif
}

0 comments on commit b357c36

Please sign in to comment.