How bennokress/prettylog works
Main Components
• Logging API (Global Functions): The “import once” approach creates global log functions (e.g., logV, logD, logI, logW, logE, fatalLog) which act as the entry points for logging. These functions are defined in a dedicated Swift file that wraps calls to the core “PrettyLogProxy”.
• PrettyLogProxy: This acts as the central gateway through which all log calls pass. It coordinates the logging process, formats messages, and delegates the calls to the appropriate log targets.
• Helpers and Core Utilities:
• LogLevel, LogCategory: These types (found in Sources/PrettyLog/Helpers) define the different log levels (verbose, debug, info, etc.) and log categories (like service, storage, manager, or custom ones). Extensions in “Type Extensions” further support functionality like string formatting.
• PrettyLog: The primary module that might encapsulate common logic and act as the façade of the package.
• LogTargets: Modules/components that are responsible for sending the log output to its destination. For example, “ConsoleLog” (in Sources/PrettyLog/Targets) is provided as the default log target that outputs to the Xcode console. This is designed using the LogTarget protocol so that additional targets (such as remote logging services or file logging) can be integrated.
• Integration Layer: This is not part of the package’s internal structure, but it is explained in the README. It shows how an app integrates the package (by either importing PrettyLog everywhere or “import once” by creating a custom proxy file).
Relationships and Data Flow
• Client Code → Global Log Functions: Developers call the global logging methods (logV, logD, etc.) in their application code.
• Global Log Methods → PrettyLogProxy: These methods delegate the actual logging responsibility to PrettyLogProxy.
• PrettyLogProxy uses Helper Components: It interacts with LogLevel and LogCategory to determine the formatting and the state of the log.
• PrettyLogProxy → Log Targets: Once the log message is properly formed, PrettyLogProxy dispatches the log statement to one or more LogTarget components. For example, the provided “ConsoleLog” handles logging to the Xcode Console.
Architectural Patterns and Design Principles
• Layered Architecture: The design separates the user-facing API (global log methods), the core logic (PrettyLogProxy and helper modules), and the output layer (log targets).
• Protocol-Based Abstraction: The LogTarget protocol abstracts how logging outputs are handled, allowing for easy substitution with custom targets.
• Extensibility: The system is built to be extended with custom log levels and categories through extensions.
• Modularity: The folder organization reveals clear separation of concerns – helpers (for core types and support methods), targets (for logging destinations), and extensions for type improvements.
Relevant Technologies/Frameworks
• Swift Package Manager: Notice the Package.swift file and the .swiftpm directory, which indicate that the package is managed via SwiftPM.
• Xcode and the Xcode Console: As the primary log output is directed to the Xcode console.
• Swift Language features and extensions: Used extensively to improve usability and extensibility.
Elements
• Client Code (the application using PrettyLog)
• Global Logging Functions (logV, logD, etc.)
• PrettyLogProxy (the central logging controller)
• Utility/Helper Components (LogLevel, LogCategory, and Type Extensions)
• From Client Code to Global Logging Functions (indicating call initiation).
• From Global Logging Functions to PrettyLogProxy.
• From PrettyLogProxy to Utility Components (for formatting and log decision logic) and simultaneously to Log Targets (for output handling).
• Rectangles for modules/components.
Final Recommendations
• Don’t be afraid to break down the architecture further (for instance, show inside PrettyLogProxy the involvement with LogLevel and LogCategory).