SwiftLogKit is a simple, flexible logging library for Swift, ideal for organizing logs in iOS, macOS, tvOS, watchOS, and visionOS projects. It supports multiple predefined categories, privacy settings, and custom logs.
- Predefined Categories: Offers common log categories like
default
,networking
,database
,authentication
, andui
. - Custom Categories: Supports flexible, temporary logging with custom categories.
- Privacy Control: Controls visibility of logs with
auto
,public
,private
, andsensitive
privacy settings. - Log Levels: Manages log importance with levels like
default
,debug
,info
,warning
, andfault
. Logger
Extension: Provides simple extensions likeLogger.default
,Logger.networking
, etc., for ease of use.
- Xcode: Version 14 or higher
- Swift: Version 5.9 or higher
- Platforms: iOS 16+, macOS 13+, tvOS 16+, watchOS 9+, visionOS 1+
Add SwiftLogKit to your project using Swift Package Manager.
- Open your Xcode project.
- Select File > Swift Packages > Add Package Dependency.
- Enter the repository URL:
https://github.com/kentanakae/SwiftLogKit.git
- Choose the latest version.
If you are using SwiftLogKit in a Swift package, add it as a dependency in your Package.swift file as follows:
dependencies: [
.package(url: "https://github.com/kentanakae/SwiftLogKit.git", from: "1.0.0")
]
Import SwiftLogKit
at the top of your Swift file.
import SwiftLogKit
Here's a basic example of using SwiftLogKit to log a simple message.
Log.default.info("Hello, SwiftLogKit!")
This will log a standard informational message using the default log category.
SwiftLogKit provides predefined categories for common logging purposes.
Log.default.info("App launched successfully")
Log.networking.debug("Fetching data from server")
Log.database.warning("Failed to save data to database")
Log.authentication.fault("User authentication failed")
Log.ui.info("Button was tapped")
Use the custom
category for specific or temporary logging needs. This category is recommended for short-term use.
let customLogger = Log.custom(category: "temporaryCategory")
customLogger.info("Custom log for a specific feature")
Alongside Log
, SwiftLogKit includes Logger
extensions for easy use of common categories.
To use the Logger extensions, make sure to import os.log
.
import os.log
Logger.default.info("General information in the default logger")
Logger.networking.debug("Network response debug information")
Logger.database.warning("Database-related warning")
Logger.authentication.fault("Critical authentication error")
Logger.ui.info("UI interaction logged")
You can also create a custom Logger
instance for temporary use.
let customLogger = Logger.custom(category: "specialFeature")
customLogger.info("Custom information for specialFeature")
The type
parameter specifies the importance level of each log message, making logs easier to filter and organize. The default setting is .default
.
Log levels available:
- Default: General-purpose logging for standard information.
- Debug: For development and debugging messages.
- Info: General information about the app's state or progress.
- Warning: Warnings about recoverable issues.
- Fault: Critical errors representing app failures.
Log.default.info("Informational message")
Log.default.warning("Caution: potential issue detected")
SwiftLogKit allows you to specify privacy settings to control the visibility of log messages. The privacy
parameter manages the level of visibility, which is especially useful in production environments. The default setting is .auto
.
Log.default.info("User data loaded successfully", privacy: .public)
Log.default.debug("Sensitive user data", privacy: .private)
Combining privacy
and type
parameters allows for detailed control over both the visibility and importance of logs. Here’s an example that uses both.
Log.default.debug("Sensitive user action data", privacy: .sensitive)
This logs a sensitive
privacy setting message at the debug
level.
The debug
, info
, warning
, and fault
methods provide simplified ways to log messages at specific levels.
Log.default.debug("Detailed debug information for app state")
Log.default.info("General information about app flow")
Log.default.warning("Warning: Potential issue in user interaction")
Log.default.fault("Critical error encountered")
SwiftLogKit's LogPrivacy
levels are based on OSLogPrivacy
.
Privacy Setting | Description |
---|---|
.auto |
Automatically adjusts privacy based on context. |
.public |
Logs content publicly. |
.private |
Logs sensitive information privately. |
.sensitive |
Treats content as sensitive and keeps it private. |
We welcome contributions to SwiftLogKit! Feel free to open issues, suggest new features, or submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for details.