Files
Ho Ngoc Hai 76d75c753b Migrate
2026-05-23 18:37:02 +07:00

174 lines
5.5 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// DebugLogger.swift
// AppClientBaseSwift
//
// Debug logging utilities
// Tin ích ghi log debug
//
import Foundation
// MARK: - Debug Logger
// Logger Debug
/// Debug logging utility
/// Tin ích ghi log debug
enum DebugLogger {
/// Log level
/// Mc đ log
enum Level: String {
case verbose = "📝"
case info = ""
case warning = "⚠️"
case error = ""
case success = ""
}
/// Log message with level
/// Ghi log message vi level
/// - Parameters:
/// - level: Log level / Mc đ log
/// - message: Log message / Thông đip log
/// - file: Source file / File ngun
/// - function: Function name / Tên function
/// - line: Line number / S dòng
static func log(
_ level: Level,
_ message: String,
file: String = #file,
function: String = #function,
line: Int = #line
) {
#if DEBUG
let fileName = (file as NSString).lastPathComponent
let timestamp = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium)
print("\(level.rawValue) [\(timestamp)] \(fileName):\(line) - \(function)")
print(" \(message)")
#endif
}
/// Log verbose message
/// Ghi log verbose
static func verbose(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(.verbose, message, file: file, function: function, line: line)
}
/// Log info message
/// Ghi log info
static func info(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(.info, message, file: file, function: function, line: line)
}
/// Log warning message
/// Ghi log warning
static func warning(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(.warning, message, file: file, function: function, line: line)
}
/// Log error message
/// Ghi log error
static func error(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(.error, message, file: file, function: function, line: line)
}
/// Log success message
/// Ghi log success
static func success(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
log(.success, message, file: file, function: function, line: line)
}
/// Log JSON data
/// Ghi log d liu JSON
/// - Parameters:
/// - data: JSON data / D liu JSON
/// - title: Optional title / Tiêu đ tùy chn
static func logJSON(_ data: Data, title: String = "JSON Response") {
#if DEBUG
guard let jsonString = String(data: data, encoding: .utf8) else {
error("Unable to convert data to string")
return
}
// Try to pretty print JSON
// Th format JSON đp
if let jsonData = jsonString.data(using: .utf8),
let jsonObject = try? JSONSerialization.jsonObject(with: jsonData),
let prettyData = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted),
let prettyString = String(data: prettyData, encoding: .utf8) {
print("📄 \(title):")
print(prettyString)
} else {
print("📄 \(title):")
print(jsonString)
}
#endif
}
/// Log API request
/// Ghi log API request
/// - Parameters:
/// - method: HTTP method / Phương thc HTTP
/// - url: Request URL / URL request
/// - headers: Request headers / Headers request
/// - body: Request body / Body request
static func logRequest(
method: String,
url: String,
headers: [String: String]? = nil,
body: Data? = nil
) {
#if DEBUG
print("🚀 API Request:")
print(" Method: \(method)")
print(" URL: \(url)")
if let headers = headers, !headers.isEmpty {
print(" Headers:")
headers.forEach { key, value in
// Hide sensitive data
// n d liu nhy cm
if key.lowercased() == "authorization" {
print(" \(key): Bearer ***")
} else {
print(" \(key): \(value)")
}
}
}
if let body = body {
print(" Body:")
logJSON(body, title: "Request Body")
}
#endif
}
/// Log API response
/// Ghi log API response
/// - Parameters:
/// - statusCode: HTTP status code / Mã trng thái HTTP
/// - data: Response data / D liu response
/// - error: Error if any / Li nếu có
static func logResponse(
statusCode: Int,
data: Data?,
error: Error? = nil
) {
#if DEBUG
if let error = error {
print("❌ API Response Error:")
print(" Status Code: \(statusCode)")
print(" Error: \(error.localizedDescription)")
} else {
let emoji = statusCode >= 200 && statusCode < 300 ? "" : "⚠️"
print("\(emoji) API Response:")
print(" Status Code: \(statusCode)")
}
if let data = data {
logJSON(data, title: "Response Data")
}
#endif
}
}