diff --git a/.github/workflows/deploy_docs.yml b/.github/workflows/deploy_docs.yml deleted file mode 100644 index fc6f983..0000000 --- a/.github/workflows/deploy_docs.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: Docs - -on: - push: - branches: - - main - workflow_dispatch: - -# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages -permissions: - contents: read - pages: write - id-token: write - -# Allow one concurrent deployment -concurrency: - group: "pages" - cancel-in-progress: true - -jobs: - docs: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Set up Pages - uses: actions/configure-pages@v1 - - name: Set up Swift - uses: fwal/setup-swift@v1 - with: - swift-version: '5.7' - - name: Generate Docs - uses: fwcd/swift-docc-action@v1 - with: - target: LoggingLoki - output: ./public - transform-for-static-hosting: 'true' - disable-indexing: 'true' - hosting-base-path: swift-log-loki - - name: Upload artifact - uses: actions/upload-pages-artifact@v1 - with: - path: ./public - - deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - needs: docs - - steps: - - name: Deploy Docs - uses: actions/deploy-pages@v1 diff --git a/Package.resolved b/Package.resolved index 3d77e17..b2fd757 100644 --- a/Package.resolved +++ b/Package.resolved @@ -140,8 +140,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-protobuf.git", "state" : { - "revision" : "ab3a58b7209a17d781c0d1dbb3e1ff3da306bae8", - "version" : "1.20.3" + "revision" : "9f0c76544701845ad98716f3f6a774a892152bcb", + "version" : "1.26.0" } }, { diff --git a/Package.swift b/Package.swift index a24ffce..5a69e8f 100644 --- a/Package.swift +++ b/Package.swift @@ -3,7 +3,7 @@ import PackageDescription let package = Package( name: "swift-log-loki", - platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v9), .visionOS(.v1)], + platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v10), .visionOS(.v1)], products: [ .library(name: "LoggingLoki", targets: ["LoggingLoki"]), ], diff --git a/README.md b/README.md index bc9398d..7b27d42 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ [![Coverage](https://codecov.io/gh/lovetodream/swift-log-loki/graph/badge.svg?token=Q70PZWS0T2)](https://codecov.io/gh/lovetodream/swift-log-loki) [![Tests](https://github.com/lovetodream/swift-log-loki/actions/workflows/tests.yml/badge.svg)](https://github.com/lovetodream/swift-log-loki/actions/workflows/tests.yml) -[![Docs](https://github.com/lovetodream/swift-log-loki/actions/workflows/deploy_docs.yml/badge.svg)](https://github.com/lovetodream/swift-log-loki/actions/workflows/deploy_docs.yml) +[![Swift Versions](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flovetodream%2Fswift-log-loki%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/lovetodream/swift-log-loki) +[![Supported Platforms](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flovetodream%2Fswift-log-loki%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/lovetodream/swift-log-loki) This library can be used as an implementation of Apple's [swift-log](https://github.com/apple/swift-log) interface that captures console logs from apps or services and sends them to [Grafana Loki](https://grafana.com/oss/loki). @@ -59,7 +60,7 @@ try await withThrowingDiscardingTaskGroup { group in ## API documentation -For more information visit the [API reference](https://timozacherl.com/swift-log-loki/documentation/loggingloki/). +For more information visit the [API reference](https://swiftpackageindex.com/lovetodream/swift-log-loki/documentation/loggingloki). ## License diff --git a/Sources/LoggingLoki/LokiLogHandler.swift b/Sources/LoggingLoki/LokiLogHandler.swift index cf64bd2..fb31d43 100644 --- a/Sources/LoggingLoki/LokiLogHandler.swift +++ b/Sources/LoggingLoki/LokiLogHandler.swift @@ -17,6 +17,8 @@ public struct LokiLogHandler: LogHandler, Sendable wh /// Static labels sent to Loki, which should not depend on the context of a log message. public var lokiLabels: [String: String] + public var metadataProvider: Logger.MetadataProvider? + /// Creates a log handler, which sends logs to Grafana Loki. /// /// @Snippet(path: "swift-log-loki/Snippets/BasicUsage", slice: "setup") @@ -28,16 +30,19 @@ public struct LokiLogHandler: LogHandler, Sendable wh /// It will be sent to Loki as the `service` label. /// - lokiLabels: Static labels sent to Loki, which should not depend on the context of a log message. /// - processor: Backend service which manages and sends logs to Loki. + /// - metadataProvider: A MetadataProvider, used to automatically inject runtime-generated metadata to all logs. public init( label: String, service: String = ProcessInfo.processInfo.processName, lokiLabels: [String: String] = [:], - processor: LokiLogProcessor + processor: LokiLogProcessor, + metadataProvider: Logger.MetadataProvider? = nil ) { self.label = label self.service = service self.lokiLabels = lokiLabels self.processor = processor + self.metadataProvider = metadataProvider } /// This method is called when a `LogHandler` must emit a log message. There is no need for the `LogHandler` to diff --git a/Tests/LoggingLokiTests/LokiLogHandlerTests.swift b/Tests/LoggingLokiTests/LokiLogHandlerTests.swift index 4878ce1..74c23dd 100644 --- a/Tests/LoggingLokiTests/LokiLogHandlerTests.swift +++ b/Tests/LoggingLokiTests/LokiLogHandlerTests.swift @@ -138,6 +138,29 @@ final class LokiLogHandlerTests: XCTestCase { processing.cancel() } + func testMetadataPreparation() { + let metadata1 = LokiLogHandler.prepareMetadata(base: [:], provider: .init({ [:] }), explicit: [:]) + XCTAssertEqual(metadata1, [:]) + let metadata2 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: .init({ [:] }), explicit: [:]) + XCTAssertEqual(metadata2, ["hello": "there"]) + let metadata3 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: .init({ ["provided": "metadata"] }), explicit: [:]) + XCTAssertEqual(metadata3, ["hello": "there", "provided": "metadata"]) + let metadata4 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: .init({ ["provided": "metadata"] }), explicit: ["explicit": "metadata"]) + XCTAssertEqual(metadata4, ["hello": "there", "provided": "metadata", "explicit": "metadata"]) + let metadata5 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: nil, explicit: ["explicit": "metadata"]) + XCTAssertEqual(metadata5, ["hello": "there", "explicit": "metadata"]) + let metadata6 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: nil, explicit: nil) + XCTAssertEqual(metadata6, ["hello": "there"]) + let metadata7 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: .init({ ["hello": "how are you"] }), explicit: nil) + XCTAssertEqual(metadata7, ["hello": "how are you"]) + let metadata8 = LokiLogHandler.prepareMetadata(base: ["hello": "there"], provider: .init({ ["hello": "how are you"] }), explicit: ["hello": "I am fine"]) + XCTAssertEqual(metadata8, ["hello": "I am fine"]) + var handler = LokiLogHandler(label: "test", processor: .init(configuration: .init(lokiURL: ""))) + handler[metadataKey: "key"] = "value" + XCTAssertEqual(handler.metadata, ["key": "value"]) + XCTAssertEqual(handler[metadataKey: "key"], "value") + } + func checkIfLogExists(for transformer: TestTransformer, file: StaticString = #filePath, line: UInt = #line) throws { let firstLog = try XCTUnwrap(transformer.logs?.first, file: file, line: line)