/
OTelLogHandler.swift
61 lines (54 loc) · 1.65 KB
/
OTelLogHandler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2024 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AsyncAlgorithms
import Logging
import NIOConcurrencyHelpers
import ServiceLifecycle
import Tracing
@_spi(Logging)
public struct OTelLogHandler: Sendable, LogHandler {
public var metadata: Logging.Logger.Metadata
public var logLevel: Logging.Logger.Level
private let processor: any OTelLogEntryProcessor
public init(
processor: any OTelLogEntryProcessor,
logLevel: Logger.Level,
metadata: Logger.Metadata = [:]
) {
self.processor = processor
self.logLevel = logLevel
self.metadata = metadata
}
public subscript(metadataKey key: String) -> Logging.Logger.Metadata.Value? {
get { metadata[key] }
set { metadata[key] = newValue }
}
public func log(
level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
source: String,
file: String,
function: String,
line: UInt
) {
let instant = DefaultTracerClock().now
let message = OTelLogEntry(
body: message.description,
level: level,
metadata: metadata,
timeNanosecondsSinceEpoch: instant.nanosecondsSinceEpoch
)
processor.onLog(message)
}
}