Giter Site home page Giter Site logo

lovetodream / swift-log-loki Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 2.0 169 KB

Grafana Loki Logging for Swift based on swift-log

Home Page: https://swiftpackageindex.com/lovetodream/swift-log-loki/documentation/loggingloki

License: Apache License 2.0

Swift 86.57% Makefile 2.20% Shell 11.23%
grafana-loki swift-log swift-vapor

swift-log-loki's Introduction

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.

swift-log-loki's People

Contributors

lovetodream avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

swift-log-loki's Issues

LokiStream.swift: Reuse NumberFormatter

Hi, I stumbled across this Swift package because I am looking for a similar solution.

I noticed that at one point you create a new NumberFormatter() for each log entry and I would like to kindly suggest that you either pull this out of the compactMap or - better - use a factory for it.

NumberFormatter() are quite expensive to create and it is recommended to reuse them as often as possible.
(see https://sarunw.com/posts/how-expensive-is-dateformatter/)

self.values = logs.compactMap { log in
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.groupingSeparator = ""
#if os(macOS) || !canImport(Darwin)
formatter.thousandSeparator = ""
#endif

One way could be

private class LokiFormatters {
    
    static let decimalFormatter = {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.groupingSeparator = ""
        #if os(macOS) || !canImport(Darwin)
        formatter.thousandSeparator = ""
        #endif
        
        return formatter
    }()
    
}


struct LokiStream: Encodable, Sendable {
    var stream: Dictionary<String, String>
    var values: Array<Array<String>>

    init(_ logs: [LokiLog], with labels: LokiLabels) {
        self.stream = labels
        self.values = logs.compactMap { log in
            let timestamp = Int64(log.timestamp.timeIntervalSince1970 * 1_000_000_000) as NSNumber
            guard let formattedTimestamp = LokiFormatters.decimalFormatter.string(from: timestamp) else {
                return nil
            }
            return [formattedTimestamp, log.message]
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.