Giter Site home page Giter Site logo

lovetodream / swift-log-loki Goto Github PK

View Code? Open in Web Editor NEW
11.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 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.