A micro framework for logging in Swift

LoggingKit

LoggingKit is a micro framework for logging based on log providers.

Features

  • [x] Define your own log providers
  • [x] Combine ready
  • [x] Comes with pre-defined OSLogProvider which uses os_log under the hood

Example

The example application is the best way to see LoggingKit in action. Simply open the LoggingKit.xcodeproj and run the Example scheme.

After the application has started you should see several log messages in your Xcode terminal and the Console.app for the device you ran the app on.

Installation

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/alexanderwe/LoggingKit.git", from: "2.0.0")
]

Alternatively navigate to your Xcode project, select Swift Packages and click the + icon to search for LoggingKit.

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate LoggingKit into your project manually. Simply drag the Sources Folder into your Xcode project.

Usage

At first it makes sense to create an extensions on LogCategories to define your own categories.

import LoggingKit

extension LogCategories {
    public var viewControllers: LogCategory { return .init("viewControllers") }
    public var networking: LogCategory { return .init("networking") }
    ...
}

Then register your log providers in the application(application:didFinishLaunchingWithOptions:).

import LoggingKit

class AppDelegate: UIResponder, UIApplicationDelegate {

    ...

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        LogService.register(logProviders: LogProvider, LogProvider ...)

    }

    ...
}

After that Simply import LoggingKit in the files you want to use the logging methods and use them accordingly

import LoggingKit

LogService.shared.debug("Hello Debug", logCategory: \.viewControllers)
LogService.shared.verbose("Hello Verbose", logCategory: \.viewControllers)
LogService.shared.info("Hello Info", logCategory: \.viewControllers)
LogService.shared.warning("Hello Warning", logCategory: \.viewControllers)
LogService.shared.error("Hello Error", logCategory: \.viewControllers)

Combine

If you are using combine, LoggingKit offers some extensions on the Publisher type to log Self.Output and Self.Failure.

You can choose whichever category you want. The \.combine category is a custom defined one.

import LoggingKit

// logs `Self.Output`
myPublisher.logValue(logType: .info, logCategory: \.combine) {
    "My Value is \($0)"
}

// logs `Self.Failure`
myPublisher.logError(logCategory: \.combine) {
    "My Error is \($0)"
}

// logs `Self.Output` as well as `Self.Failure`
myPublisher.log()

Providers

The idea behind this small framework is, that you can extend it by writing your own log providers by conforming to the LogProvider protocol. These implementations then can be registered in the LogService.register(providers:) method.

You can find an example LogProvider implementation in ./Example/MyTestLogProvider.swift

OSLogProvider

LoggingKit comes with one pre-defined OSLogProvider . It uses os_log under the hood to log your messages. These messages can then be viewed in the Console.app application of your mac and on the console in Xcode.

Console App

Open Console.App on your mac, select the device from which you want to view the log messages, to view the messages printed by the OSLogProvider

GitHub