Skip to content

lovetodream/swift-log-loki

SwiftLogLoki

Coverage Documentation Apache 2.0 License CI Swift Versions Supported Platforms

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 Linux and all Apple platforms
  • Different logging levels such as trace, debug, info, notice, warning, error and critical
  • Option to send logs as snappy-compressed Protobuf (default) or JSON
  • Send logs in batches via Duration since batch creation, amount of log entries in batch or a mix of both 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: "2.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 Logging
import LoggingLoki

let processor = LokiLogProcessor(
    configuration: LokiLogProcessorConfiguration(lokiURL: "http://localhost:3100")
)
LoggingSystem.bootstrap { label in
    LokiLogHandler(label: label, processor: processor)
}

try await withThrowingDiscardingTaskGroup { group in
    group.addTask {
        // The processor has to run in the background to send logs to Loki.
        try await processor.run()
    }
}

API documentation

For more information visit the API reference.

License

Apache 2.0

Copyright (c) 2022-present, Timo Zacherl (@lovetodream)

This project contains code written by others not affliated with this project. All copyright claims are reserved by them. For a full list, with their claimed rights, see NOTICE.txt

Swift is a registered trademark of Apple, Inc. Any use of their trademark does not imply any affiliation with or endorsement by them, and all rights are reserved by them.