A lightweight logging utility for iOS, macOS, watchOS and tvOS.
- Swift 5 support
- Modern syntax
- OSLog support
- Custom logging destinations
Create a Package.swift
file, and add this snippet:
import PackageDescription
let package = Package(
name: "HelloWorld",
dependencies: [
.Package(url: "https://github.com/bojan/Troop.git")
]
)
Add this to your Cartfile
:
github "bojan/Troop"
Add the repository as a submodule to your project.
git submodule add https://github.com/bojan/Troop.git Vendor/Troop
Open the newly created folder in Finder and drag Troop.xcodeproj
to your project.
In your project's settings, select your target and under General > Embedded Binaries, add the framework depending on the target OS (iOS, macOS, watchOS or tvOS).
Import the module where needed:
import Troop
let configuration: Troop.Configuration = {
var configuration = Troop.Configuration(prefix: "some-prefix", level: .debug)
#if DEBUG
configuration.level = .verbose
#endif
return configuration
}()
let log = Troop(configuration: Logger.configuration, destination: UnifiedLogDestination(subsystem: configuration.prefix, category: "Your App"))
Use any of the following logging statements according to the level you need to be logged:
log.verbose(message)
log.info(message)
log.error(message)
log.debug(message)
log.warning(message)
In order to avoid using global variables and importing the framework in multiple files, use this wrapper:
struct Logger {
private static let configuration: Troop.Configuration = {
var configuration = Troop.Configuration(prefix: "some-prefix", level: .debug)
#if DEBUG
configuration.level = .verbose
#endif
return configuration
}()
private static let log = Troop(configuration: Logger.configuration, destination: UnifiedLogDestination(subsystem: configuration.prefix, category: "Your App"))
static func verbose(_ message: String) {
log.verbose(message)
}
static func info(_ message: String) {
log.info(message)
}
static func error(_ message: String) {
log.error(message)
}
static func debug(_ message: String) {
log.debug(message)
}
static func warning(_ message: String) {
log.warning(message)
}
}
All contributions and suggestions are welcome and very much appreciated.
Should you have a feature request or a problem that you may experience, feel free to open an issue.
If you are willing to contribute by adding a feature or squashing a bug or two, please submit a pull request.
Bojan Dimovski
Troop is available under the WTFPL license. Check the LICENSE file for more info.