<img src=”https://github.com/gokulnair2001/OSLogKit/assets/56252259/53164f76-9f0e-4dbf-8298-814424a427a0” width=20% height=20%>
OSLogKit is a layer 2 framework developed on top of the native OSLog system, aiming to simplify the integration of OSLog into your project. Apple has done an excellent job in ensuring that OSLogging is straightforward and easy to use. However, when it comes to implementing OSLog in your codebase, can be challenging.
To address this, OSLogKit has been developed, further simplifying the process of adopting OSLog. One notable advantage of OSLogKit is that it offers additional functionalities not available in the native API.
OSLogKit has a variety of Modifiers which makes OSLogKit adoption very easy. Don’t worry if your code base is in UIKit, you can still adopt it quickly.
In order to use Logger you need to initialize the instance of Logger. OSLogKit supports a variety of Initialisation
let logger = Logger()
let logger = Logger(subSystem: "com.gokulnair.Test-OSKit", category: "ContentView")
Following are the methods which OSLogKit supports
💽 Single Capture/Logging
.default
func capture(level: OSLogType? = nil, message: String)
💽 Bulk Capturing/Logging
func captureInBulk(levels: [OSLogType] = [], messages: [String])
💽 Log Exporting
subsystem
using which the logs are madetimeSpan
denotes the span for which logs needs to be exported(Default 1 day)completion
Block returns an array of logs captured if the process succeeds and on failure, OSLogKit provides the error.[[Date] [Category] message]
func exportLogs(forSubsystem subsystem: String = "", timeSpan span: OSLogSpan = .day(1), completion: @escaping((Result<[String], OSLogError>) -> ()))
### OSLogSpan
Name | Cases | Description |
---|---|---|
minutes | .minute(x) | Considers x minutes in past |
hours | .hour(x) | Considers x hours in past |
days | .day(x) | Considers x days in past |
### OSLogError
errorDescription
in the failure case in order to get the error descriptionName | Case | Description |
---|---|---|
Export Failure | .exportFailure(let customString) | Custom export failure error |
Invalid Subsystem | .invalidSubsystem | Occurs when an Invalid Subsystem is used |
## SwiftUI Modifiers
### 🧩 CaptureLogOnAppear
logger
log instance for which logs need to be capturedmessage
string which needs to be loggedlevel
specific level on which log needs to be capturedperform
Since this modifier is made on top of OnAppear in order to perform additional actions in OnAppear you can use this param
func captureLogOnAppear(_ logger: OSLogKit, _ message: String, _ level: OSLogType = .default, _ perform: (() -> Void)? = nil)
### 🧩 CaptureBulkLogOnAppear
logger
log instance for which logs need to be capturedmessage
strings that need to be loggedlevel
specific levels on which logs need to be capturedperform
Since this modifier is made on top of OnAppear in order to perform additional actions in OnAppear you can use this param
func captureBulkLogOnAppear(_ logger: OSLogKit, _ messages: [String], _ levels: [OSLogType], _ perform: (() -> Void)? = nil)
Other modifiers are
Case | Description |
---|---|
.captureLogOnDisappear | Captures log on disappear |
.captureBulkLogOnDisappear | Captures bulk log on disappear |
.captureLogOnLongPress | Captures log on long press |
.captureBulkLogOnLongPress | Captures bulk log on long press |
.captureLogOnTap | Captures log on tap |
.captureBulkLogOnTap | Captures bulk log on tap |
.captureLogOnSubmit | Captures log on submit |
.captureBulkLogOnSubmit | Captures bulk log on submit |
CaptureLogOnAppear
& captureBulkLogOnAppear
VStack {
// Some view
}.captureLogOnTap(logger, "OSLogKit test log") {
// additional on tap action (Optional)
}
logger.capture(message: "OSLogKit test log")
OSLogKit is available through Swift Package Manager. To add OSLogKit through SPM
File > Add Packages
https://github.com/gokulnair2001/OSLogKit
<img src=”https://github.com/gokulnair2001/OSLogKit/assets/56252259/05f1688b-b62a-4114-a239-aa2f691f2e52” width=80% height=40%>
Made with ❤️ in 🇮🇳 By Gokul Nair