Introduction
XLFacility is a versatile logging framework for Objective-C and Swift. It is designed to work seamlessly across various Apple platforms such as macOS, iOS, watchOS, and tvOS.
Key Features
- Efficient and lightweight logging framework
- Supports both Objective-C and Swift
- Compatibility across multiple Apple platforms
- Flexible log level filtering
- Customizable logging destinations
- Thread-safe operations
- Easy to integrate into existing projects
Installation
Installing XLFacility is straightforward using CocoaPods.
- Add the following line to your Podfile:
“`ruby
pod ‘XLFacility’
“`
- Run the command
pod install
in your project directory.
Usage
Before you start using XLFacility in your project, make sure you import the library.
“`swift
import XLFacility
“`
Logging Messages
To log a message, simply call the appropriate logging function:
“`swift
XLLogger.log(“This is a log message.”)
XLLogger.debug(“This is a debug message.”)
XLLogger.info(“This is an info message.”)
XLLogger.warning(“This is a warning message.”)
XLLogger.error(“This is an error message.”)
“`
Log Level Filtering
XLFacility allows you to filter log messages based on their severity level. By default, all log levels are enabled.
To set a specific log level, use the following code:
“`swift
XLLogger.logLevel = .info
“`
This will only output log messages with a severity level equal to or higher than info
. You can choose from the following log levels:
.off
– No logging.error
– Only error messages.warning
– Includes warning and error messages.info
– Includes info, warning, and error messages.debug
– Includes debug, info, warning, and error messages.log
– Logs all messages (default level)
Custom Logging Destinations
XLFacility provides multiple logging destinations, allowing you to configure where the log messages get stored.
To add a custom logging destination, follow these steps:
- Create a class that conforms to the
XLLogDestination
protocol. - Implement the required methods that handle logging.
- Add the new destination to the logger using the
addDestination(_:)
method.
Example: Adding a File Logger
Here’s an example of how to create a file logger using XLFacility:
- Create a class that conforms to the
XLFileLogDestination
protocol. - Implement the required methods:
“`swift
class FileLogger: XLFileLogDestination {
var logDirectoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
func log(_ details: XLLogRecordDetails, message: String) {
// Implement your logging logic here, e.g., save the log to a file
// Make sure to handle the logDirectoryURL appropriately
}
func supports(_ logLevel: XLLogLevel) -> Bool {
// Implement which log levels this destination supports
return true // Or perform your own filtering logic
}
}
“`
- Add the destination to the logger:
“`swift
let fileLogger = FileLogger()
XLLogger.addDestination(fileLogger)
“`
Contribute
XLFacility is an open-source project, and contributions are welcome!
If you encounter any issues, have suggestions for improvements, or would like to contribute code, please visit the GitHub repository for XLFacility.
License
XLFacility is released under the MIT License.