Skip to content

Commit

Permalink
feat: add labels + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Apr 21, 2024
1 parent 3628e61 commit 942ca64
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 25 additions & 6 deletions Sources/LoggingLoki/LokiLogHandler.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import class Foundation.ProcessInfo
import Logging

/// ``LokiLogHandler`` is a logging backend for `Logging`.
public struct LokiLogHandler<Clock: _Concurrency.Clock>: LogHandler, Sendable where Clock.Duration == Duration {

private let processor: LokiLogProcessor<Clock>

/// The service label for the log handler instance.
/// The logger label for the log handler instance.
///
/// This value will be sent to Grafana Loki as the `service` label.
/// This value will be sent to Grafana Loki as the `logger` label.
public var label: String
/// The service/program name.
///
/// This value will be sent to Grafana Loki as the `service` label.
public var service: String
/// Static labels sent to Loki, which should not depend on the context of a log message.
public var lokiLabels: [String: String]

/// Creates a log handler, which sends logs to Grafana Loki.
///
/// @Snippet(path: "swift-log-loki/Snippets/BasicUsage", slice: "setup")
///
/// - Parameters:
/// - label: Client supplied string describing the logger. Should be unique but not enforced
/// - label: Client supplied string describing the logger. Should be unique but not enforced.
/// It's also sent to Loki as the `logger` label.
/// - service: Client supplied string indicating the service/program name.
/// 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.
public init(label: String, processor: LokiLogProcessor<Clock>) {
public init(
label: String,
service: String = ProcessInfo.processInfo.processName,
lokiLabels: [String: String] = [:],
processor: LokiLogProcessor<Clock>
) {
self.label = label
self.service = service
self.lokiLabels = lokiLabels
self.processor = processor
}

Expand Down Expand Up @@ -50,12 +68,13 @@ public struct LokiLogHandler<Clock: _Concurrency.Clock>: LogHandler, Sendable wh
)

let labels = [
"service": label,
"service": service,
"logger": label,
"source": source,
"file": file,
"function": function,
"line": String(line)
]
].merging(lokiLabels) { old, _ in old } // message specific labels win!

processor.addEntryToBatch(.init(
timestamp: .init(),
Expand Down
6 changes: 3 additions & 3 deletions Tests/LoggingLokiTests/LokiLogProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ final class LokiLogProcessorTests: XCTestCase {
func testLogFmtFormat() {
let configuration = LokiLogProcessorConfiguration(
lokiURL: "http://localhost:3100",
metadataFormat: .logfmt
logFormat: .logfmt
)
let processor = LokiLogProcessor(configuration: configuration)
let raw = LokiLog(
Expand All @@ -25,7 +25,7 @@ final class LokiLogProcessorTests: XCTestCase {
func testCustomFormat() {
let configuration = LokiLogProcessorConfiguration(
lokiURL: "http://localhost:3100",
metadataFormat: .custom({ level, message, metadata in
logFormat: .custom({ level, message, metadata in
"\(level.rawValue.uppercased()): \(message) [\(metadata.sorted(by: { $0.key < $1.key }).map({ "\($0.key): \($0.value)" }).joined(separator: ", "))]"
})
)
Expand All @@ -44,7 +44,7 @@ final class LokiLogProcessorTests: XCTestCase {
func testLogFmtFormatEmptyMetadata() {
var configuration = LokiLogProcessorConfiguration(
lokiURL: "http://localhost:3100",
metadataFormat: .logfmt
logFormat: .logfmt
)
configuration.encoding = .json
let processor = LokiLogProcessor(configuration: configuration)
Expand Down

0 comments on commit 942ca64

Please sign in to comment.