Some checks failed
Publish Swift Package / build-test-publish (push) Failing after 17s
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
116 lines
3.6 KiB
Swift
Executable file
116 lines
3.6 KiB
Swift
Executable file
//
|
|
// AppLogger.swift
|
|
// iOS Foundations
|
|
//
|
|
// Centralized logging with subsystem-based categorization
|
|
//
|
|
|
|
import Foundation
|
|
import OSLog
|
|
|
|
/// Centralized logging system for the application
|
|
///
|
|
/// This enum provides categorized loggers using Apple's unified logging system (OSLog).
|
|
/// Each category creates a separate logger that can be filtered in Console.app or Instruments.
|
|
///
|
|
/// ## Usage
|
|
///
|
|
/// ```swift
|
|
/// // Use predefined loggers
|
|
/// AppLogger.network.info("Fetching user profile")
|
|
/// AppLogger.ui.debug("Rendering stream view")
|
|
///
|
|
/// // Create custom category logger
|
|
/// let streamLogger = AppLogger.logger(for: "Streaming")
|
|
/// streamLogger.info("Stream started: \(streamID)")
|
|
///
|
|
/// // Log memory usage
|
|
/// AppLogger.logMemoryUsage(label: "After loading profile")
|
|
/// ```
|
|
///
|
|
/// ## Console Filtering
|
|
///
|
|
/// In Console.app, filter logs by subsystem or category:
|
|
/// - Subsystem: Your bundle identifier (e.g., "com.transftw.portal")
|
|
/// - Category: General, Network, UI, etc.
|
|
///
|
|
public enum AppLogger {
|
|
|
|
// MARK: - Subsystem
|
|
|
|
/// The app's bundle identifier, used as the logging subsystem
|
|
private static let subsystem = Bundle.main.bundleIdentifier ?? "app"
|
|
|
|
// MARK: - Predefined Loggers
|
|
|
|
/// General application logs
|
|
public static let general = Logger(subsystem: subsystem, category: "General")
|
|
|
|
/// Network-related logs (API calls, responses, errors)
|
|
public static let network = Logger(subsystem: subsystem, category: "Network")
|
|
|
|
/// UI-related logs (view lifecycle, user interactions)
|
|
public static let ui = Logger(subsystem: subsystem, category: "UI")
|
|
|
|
/// Data persistence logs (database, file I/O, UserDefaults)
|
|
public static let persistence = Logger(subsystem: subsystem, category: "Persistence")
|
|
|
|
/// Authentication and authorization logs
|
|
public static let auth = Logger(subsystem: subsystem, category: "Auth")
|
|
|
|
/// Media processing logs (audio, video, images)
|
|
public static let media = Logger(subsystem: subsystem, category: "Media")
|
|
|
|
/// Real-time updates and streaming logs
|
|
public static let realtime = Logger(subsystem: subsystem, category: "RealTime")
|
|
|
|
// MARK: - Custom Logger Factory
|
|
|
|
/// Create a logger for a custom category
|
|
///
|
|
/// - Parameter category: The category name for the logger
|
|
/// - Returns: A configured Logger instance
|
|
///
|
|
/// ## Example
|
|
///
|
|
/// ```swift
|
|
/// let paymentLogger = AppLogger.logger(for: "Payment")
|
|
/// paymentLogger.info("Processing payment")
|
|
/// ```
|
|
///
|
|
public static func logger(for category: String) -> Logger {
|
|
Logger(subsystem: subsystem, category: category)
|
|
}
|
|
|
|
// MARK: - Memory Profiling
|
|
|
|
/// Log current memory usage with a label
|
|
///
|
|
/// Useful for tracking memory consumption at critical points in your app.
|
|
///
|
|
/// - Parameter label: A descriptive label for this measurement
|
|
///
|
|
/// ## Example
|
|
///
|
|
/// ```swift
|
|
/// AppLogger.logMemoryUsage(label: "Before image processing")
|
|
/// processLargeImage()
|
|
/// AppLogger.logMemoryUsage(label: "After image processing")
|
|
/// ```
|
|
///
|
|
public static func logMemoryUsage(label: String) {
|
|
let usage = MemoryProfiler.currentUsage()
|
|
general.info("[\(label)] Memory: \(usage.formattedMB)")
|
|
}
|
|
|
|
/// Log memory usage with a custom logger
|
|
///
|
|
/// - Parameters:
|
|
/// - label: A descriptive label for this measurement
|
|
/// - logger: The logger to use for output
|
|
///
|
|
public static func logMemoryUsage(label: String, using logger: Logger) {
|
|
let usage = MemoryProfiler.currentUsage()
|
|
logger.info("[\(label)] Memory: \(usage.formattedMB)")
|
|
}
|
|
}
|