Skip to content

Commit

Permalink
Repository cleanup (#10)
Browse files Browse the repository at this point in the history
* ci: report coverage to codecov

* feat!: complete overhaul

includes various breaking changes!

* fix: missing import

* feat: cleanup

* test: more tests

* fix: test

* test: more tests and cleanup

* ci: fix stuff

* docs: update docs

* feat: add labels + fix tests

* test: fix tests
  • Loading branch information
lovetodream authored Apr 21, 2024
1 parent fb800a4 commit f981896
Show file tree
Hide file tree
Showing 43 changed files with 1,712 additions and 11,689 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ on:
branches: [ "main" ]

jobs:
build:
test:
container:
image: swift:5.9-jammy

runs-on: ubuntu-latest

services:
loki:
image: grafana/loki:3.0.0

steps:
- uses: actions/checkout@v3
- name: Build
run: swift build -v
run: swift build
- name: Run tests
run: swift test -v
run: swift test --enable-code-coverage
env:
XCT_LOKI_URL: http://loki:3100
- name: Prepare Code Coverage
run: llvm-cov export -format="lcov" .build/debug/swift-log-lokiPackageTests.xctest -instr-profile .build/debug/codecov/default.profdata -ignore-filename-regex="\.pb\.swift" > info.lcov
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: info.lcov
5 changes: 5 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1
builder:
configs:
- documentation_targets: [LoggingLoki]

54 changes: 54 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Adapted from https://www.thapaliya.com/en/writings/well-documented-makefiles/
.PHONY: help
help: ## Display this help and any documented user-facing targets. Other undocumented targets may be present in the Makefile.
help:
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make <target>\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " %-45s %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := all
.PHONY: all clean-protos protos
.PHONY: clean-protos

SHELL = /usr/bin/env bash -o pipefail

#############
# Variables #
#############

# We don't want find to scan inside a bunch of directories, to accelerate the
# 'make: Entering directory '/src/loki' phase.
DONT_FIND := -name .swiftpm -prune -o -name .build -prune -o

# Protobuf files
PROTO_DEFS := $(shell find . $(DONT_FIND) -type f -name '*.proto' -print)
PROTO_SWIFTS := $(patsubst %.proto,%.pb.swift,$(PROTO_DEFS))

################
# Main Targets #
################
all: protos ## run all (clean-protos, protos)

# This is really a check for the CI to make sure generated files are built and checked in manually
check-generated-files: protos
@if ! (git diff --exit-code $(PROTO_DEFS) $(PROTO_SWIFTS)); then \
echo "\nChanges found in generated files"; \
echo "Run 'make check-generated-files' and commit the changes to fix this error."; \
echo "If you are actively developing these files you can ignore this error"; \
echo "(Don't forget to check in the generated files when finished)\n"; \
exit 1; \
fi

#########
# Clean #
#########

clean-protos: ## remove swift protos
rm -rf $(PROTO_SWIFTS)

#############
# Protobufs #
#############

protos: clean-protos $(PROTO_SWIFTS) ## regenerate swift protos

%.pb.swift:
protoc --swift_out=. $(patsubst %.pb.swift,%.proto,$@)
132 changes: 129 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,21 +1,138 @@
{
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "fb308ee72f3d4c082a507033f94afa7395963ef3",
"version" : "1.21.0"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-async-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms.git",
"state" : {
"revision" : "da4e36f86544cdf733a40d59b3a2267e3a7bbf36",
"version" : "1.0.0"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin.git",
"state" : {
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"revision" : "26ac5758409154cc448d7ab82389c520fa8a8247",
"version" : "1.3.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65",
"version" : "1.0.3"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version" : "1.4.4"
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982",
"version" : "2.64.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "a3b640d7dc567225db7c94386a6e71aded1bfa63",
"version" : "1.22.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "0904bf0feb5122b7e5c3f15db7df0eabe623dd87",
"version" : "1.30.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "7c381eb6083542b124a6c18fae742f55001dc2b5",
"version" : "2.26.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "6cbe0ed2b394f21ab0d46b9f0c50c6be964968ce",
"version" : "1.20.1"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
Expand All @@ -27,6 +144,15 @@
"version" : "1.20.3"
}
},
{
"identity" : "swift-service-lifecycle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
"state" : {
"revision" : "d7fe0e731499a8dcce53bf4cbbc812c8e565d3a7",
"version" : "2.4.1"
}
},
{
"identity" : "swift-snappy",
"kind" : "remoteSourceControl",
Expand Down
10 changes: 7 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
import PackageDescription

let package = Package(
name: "swift-log-loki",
platforms: [.macOS(.v11), .iOS(.v14), .tvOS(.v14), .watchOS(.v7)],
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17), .watchOS(.v9), .visionOS(.v1)],
products: [
.library(name: "LoggingLoki", targets: ["LoggingLoki"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.6.0"),
.package(url: "https://github.com/lovetodream/swift-snappy", from: "1.0.0"),
.package(url: "https://github.com/lovetodream/swift-snappy.git", from: "1.0.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
],
targets: [
.target(
Expand All @@ -20,6 +22,8 @@ let package = Package(
.product(name: "Logging", package: "swift-log"),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "Snappy", package: "swift-snappy"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
]
),
.testTarget(name: "LoggingLokiTests", dependencies: ["LoggingLoki"]),
Expand Down
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# SwiftLogLoki

[![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)

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).

## Features

- Supports Darwin (macOS), Linux platforms, iOS, watchOS and tvOS
- 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
- Batching logs via `TimeInterval`, amount of log entries or a mix of those options
Expand All @@ -19,7 +20,7 @@ This library can be used as an implementation of Apple's [swift-log](https://git
Add `LoggingLoki` to the dependencies within your application's `Package.swift` file.

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

Add `LoggingLoki` to your target's dependencies.
Expand All @@ -38,26 +39,24 @@ Go to `File` > `Add Packages`, enter the Package URL `https://github.com/lovetod
You can use LoggingLoki as your default Log Handler for [swift-log](https://github.com/apple/swift-log).

```swift
import LoggingLoki
import Logging
import LoggingLoki

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

### Example Usage with [Swift Vapor](https://vapor.codes)

LoggingLoki works great with [Swift Vapor](https://vapor.codes), to send all your logs to [Grafana Loki](https://grafana.com/oss/loki) add the following to the top of your `configure(_:)` method inside of `configure.swift`.

```swift
app.logger = Logger(label: app.logger.label, factory: { label in
// yourLokiURL: e.g. http://localhost:3100 as URL
LokiLogHandler(label: label, lokiURL: yourLokiURL)
})
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()
}
}
```

For more information about Logging in [Swift Vapor](https://vapor.codes) take a look at the [Official Documentation](https://docs.vapor.codes/basics/logging/).

## API documentation

For more information visit the [API reference](https://timozacherl.com/swift-log-loki/documentation/loggingloki/).
Expand Down
19 changes: 19 additions & 0 deletions Snippets/BasicUsage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

// snippet.setup
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()
}
}
// snippet.end
Loading

0 comments on commit f981896

Please sign in to comment.