Skip to content

lovetodream/swift-log-loki

Repository files navigation

SwiftLogLoki

Tests Docs

This library can be used as an implementation of Apple's swift-log interface that captures console logs from apps or services and sends them to Grafana Loki.

Features

  • Supports Darwin (macOS), Linux platforms, iOS, watchOS and tvOS
  • Different logging levels such as trace, debug, info, notice, warning, error and critical
  • Option to send logs as snappy-compressed Protobuf (default) or JSON
  • Batching logs via TimeInterval, amount of log entries or a mix of those options

Add dependency

Swift Package

Add LoggingLoki to the dependencies within your application's Package.swift file.

.package(url: "https://github.com/lovetodream/swift-log-loki.git", from: "1.0.0"),

Add LoggingLoki to your target's dependencies.

.product(name: "LoggingLoki", package: "swift-log-loki"),

Xcode Project

Go to File > Add Packages, enter the Package URL https://github.com/lovetodream/swift-log-loki.git and press Add Package.

Usage

You can use LoggingLoki as your default Log Handler for swift-log.

import LoggingLoki
import Logging

// yourLokiURL: e.g. http://localhost:3100 as URL
LoggingSystem.bootstrap { LokiLogHandler(label: $0, lokiURL: yourLokiURL) }

Example Usage with Swift Vapor

LoggingLoki works great with Swift Vapor, to send all your logs to Grafana Loki add the following to the top of your configure(_:) method inside of configure.swift.

app.logger = Logger(label: app.logger.label, factory: { label in
    // yourLokiURL: e.g. http://localhost:3100 as URL
    LokiLogHandler(label: label, lokiURL: yourLokiURL)
})

For more information about Logging in Swift Vapor take a look at the Official Documentation.

API documentation

For more information visit the API reference.

License

This library is licensed under the MIT license. Full license text is available in LICENSE.