Skip to content

Commit 435f4cb

Browse files
feat: add standard output logger for debug (#16)
Add Standard output logger for debug options
1 parent f3ac32d commit 435f4cb

File tree

6 files changed

+124
-8
lines changed

6 files changed

+124
-8
lines changed

ExampleApp/ExampleApp/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let config = { () -> LDConfig in
99
autoEnvAttributes: .enabled
1010
)
1111
config.plugins = [
12-
Observability(options: .init(sessionBackgroundTimeout: 3))
12+
Observability(options: .init(sessionBackgroundTimeout: 3, isDebug: true))
1313
]
1414
return config
1515
}()

ExampleApp/ExampleApp/Instrumentation/Manual/InstrumentationView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ struct InstrumentationView: View {
66
var body: some View {
77
VStack {
88
TraceView()
9+
LogsView()
10+
Spacer()
911
}
1012
.padding()
1113
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import SwiftUI
2+
import LaunchDarklyObservability
3+
import OpenTelemetryApi
4+
5+
6+
struct LogsView: View {
7+
@State private var message: String = ""
8+
@State private var pressed = false
9+
10+
var body: some View {
11+
VStack(alignment: .leading, spacing: 4) {
12+
Text("Logs")
13+
.bold()
14+
HStack {
15+
TextField(text: $message) {
16+
Text("Message:")
17+
}
18+
.autocorrectionDisabled(true)
19+
.textCase(.lowercase)
20+
.textInputAutocapitalization(.never)
21+
.textFieldStyle(.roundedBorder)
22+
Spacer()
23+
Button {
24+
pressed.toggle()
25+
} label: {
26+
if pressed {
27+
ProgressView {
28+
Text("...")
29+
}
30+
} else {
31+
Text("Log button pressed")
32+
}
33+
}
34+
.buttonStyle(.borderedProminent)
35+
.accessibilityIdentifier("logsView-send-button")
36+
.disabled(message.isEmpty)
37+
.task(id: pressed) {
38+
guard pressed else {
39+
return
40+
}
41+
LDObserve.shared.recordLog(
42+
message: message,
43+
severity: .info,
44+
attributes: [
45+
"user_id": .string("1234"),
46+
"action": .string("logsView-send-button-pressed")
47+
]
48+
)
49+
pressed.toggle()
50+
message = ""
51+
}
52+
}
53+
}
54+
}
55+
}

ExampleApp/ExampleApp/Instrumentation/Manual/TraceView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct TraceView: View {
1616
TextField(text: $name) {
1717
Text("Span name:")
1818
}
19+
.autocorrectionDisabled(true)
1920
.textCase(.lowercase)
2021
.textInputAutocapitalization(.never)
2122
.textFieldStyle(.roundedBorder)

Sources/Observability/InstrumentationManager.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Foundation
22

3+
import os
4+
35
import UIKit.UIWindow
46

57
import OpenTelemetrySdk
@@ -19,7 +21,7 @@ private let metricsPath = "/v1/metrics"
1921
final class InstrumentationManager {
2022
private let sdkKey: String
2123
private let options: Options
22-
let otelLogger: Logger?
24+
let otelLogger: OpenTelemetryApi.Logger?
2325
let otelTracer: Tracer?
2426
let otelMeter: (any Meter)?
2527
public let otelBatchLogRecordProcessor: BatchLogRecordProcessor?
@@ -59,12 +61,25 @@ final class InstrumentationManager {
5961
}
6062
}
6163
.map { url in
62-
SamplingLogExporterDecorator(
63-
exporter: OtlpHttpLogExporter(
64-
endpoint: url,
65-
envVarHeaders: options.customHeaders
66-
),
67-
sampler: sampler
64+
MultiLogRecordExporter(
65+
logRecordExporters: options.isDebug ? [
66+
SamplingLogExporterDecorator(
67+
exporter: OtlpHttpLogExporter(
68+
endpoint: url,
69+
envVarHeaders: options.customHeaders
70+
),
71+
sampler: sampler
72+
),
73+
LDStdoutExporter(loggerName: options.loggerName)
74+
] : [
75+
SamplingLogExporterDecorator(
76+
exporter: OtlpHttpLogExporter(
77+
endpoint: url,
78+
envVarHeaders: options.customHeaders
79+
),
80+
sampler: sampler
81+
)
82+
]
6883
)
6984
}
7085
.map { exporter in
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
3+
import os
4+
5+
import OpenTelemetrySdk
6+
import OpenTelemetryApi
7+
8+
import Common
9+
10+
11+
final class LDStdoutExporter: LogRecordExporter {
12+
private let loggerName: String
13+
14+
init(loggerName: String) {
15+
self.loggerName = loggerName
16+
}
17+
18+
public func forceFlush(
19+
explicitTimeout: TimeInterval?
20+
) -> ExportResult {
21+
.success
22+
}
23+
24+
public func shutdown(
25+
explicitTimeout: TimeInterval?
26+
) {
27+
28+
}
29+
30+
public func export(
31+
logRecords: [ReadableLogRecord],
32+
explicitTimeout: TimeInterval?
33+
) -> ExportResult {
34+
35+
for log in logRecords {
36+
guard let message = JSON.stringify(log) else { continue }
37+
38+
os_log("%{public}@", log: .default, type: .info, message)
39+
}
40+
41+
return .success
42+
}
43+
}

0 commit comments

Comments
 (0)