/Troop

A lightweight logging utility for iOS, macOS, watchOS and tvOS.

Primary LanguageSwiftDo What The F*ck You Want To Public LicenseWTFPL

Troop

A lightweight logging utility for iOS, macOS, watchOS and tvOS.

Build Status GitHub license Carthage compatible SPM codecov

Features

  • Swift 5 support
  • Modern syntax
  • OSLog support
  • Custom logging destinations

Requirements

Installation

Swift Packager Manager (SPM)

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")
    ]
)

Carthage

Add this to your Cartfile:

github "bojan/Troop"

Manually

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).

Usage

Import the module where needed:

import Troop

Configuration

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"))

Logging statements

Use any of the following logging statements according to the level you need to be logged:

Verbose

log.verbose(message)

Info

log.info(message)

Error

log.error(message)

Debug

log.debug(message)

Warning

log.warning(message)

Example wrapper

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)
	}

}

Contributions

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.

Author

Bojan Dimovski

License

Troop is available under the WTFPL license. Check the LICENSE file for more info.