Skip to content

Commit fb9f85c

Browse files
authored
Updated layout and error states for logging (#77)
* Updated layout and error states for logging * Fixed Linux test
1 parent 92e4ac4 commit fb9f85c

File tree

10 files changed

+535
-289
lines changed

10 files changed

+535
-289
lines changed

Segment.xcodeproj/project.pbxproj

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@
4949
46FE4D1D25A7A850003A7362 /* Storage_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46FE4D1C25A7A850003A7362 /* Storage_Tests.swift */; };
5050
9620862C2575C0C800314F8D /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9620862B2575C0C800314F8D /* Events.swift */; };
5151
96208650257AA83E00314F8D /* iOSLifecycleMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9620864F257AA83E00314F8D /* iOSLifecycleMonitor.swift */; };
52-
96259F8326CEF526008AE301 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96259F8226CEF526008AE301 /* Logger.swift */; };
52+
96259F8326CEF526008AE301 /* LogTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96259F8226CEF526008AE301 /* LogTarget.swift */; };
5353
96259F8626CF1D45008AE301 /* ConsoleTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96259F8526CF1D45008AE301 /* ConsoleTarget.swift */; };
5454
96469AF82706225900AC5772 /* SystemTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96469AF72706225900AC5772 /* SystemTarget.swift */; };
5555
966945D7259BDCDD00271339 /* HTTPClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40ED259A7311008EB0B6 /* HTTPClient.swift */; };
56-
967C40DA258D472C008EB0B6 /* Logger_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40D9258D472C008EB0B6 /* Logger_Tests.swift */; };
56+
967C40DA258D472C008EB0B6 /* SegmentLog_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40D9258D472C008EB0B6 /* SegmentLog_Tests.swift */; };
5757
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */; };
5858
9692724E25A4E5B7009B5298 /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692724D25A4E5B7009B5298 /* Startup.swift */; };
5959
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692726725A583A6009B5298 /* SegmentDestination.swift */; };
60-
96C33A9C25880A5E00F3D538 /* SegmentLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */; };
61-
96C33AAC25892D6D00F3D538 /* Metrics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AAB25892D6D00F3D538 /* Metrics.swift */; };
60+
96C33A9C25880A5E00F3D538 /* SegmentLog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* SegmentLog.swift */; };
6261
96C33AB1258961F500F3D538 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AB0258961F500F3D538 /* Settings.swift */; };
62+
96C95B16271DE22700C3EB9A /* LogTarget_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C95B15271DE22600C3EB9A /* LogTarget_Tests.swift */; };
6363
96DBF37B26F39B5500724B0B /* Timeline_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */; };
6464
A31A16262576B6F200C9CDDF /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = A31A16252576B6F200C9CDDF /* Timeline.swift */; };
6565
A31A162F2576B73F00C9CDDF /* State.swift in Sources */ = {isa = PBXBuildFile; fileRef = A31A162E2576B73F00C9CDDF /* State.swift */; };
@@ -147,18 +147,18 @@
147147
9620862B2575C0C800314F8D /* Events.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Events.swift; sourceTree = "<group>"; };
148148
962086482579CCC200314F8D /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
149149
9620864F257AA83E00314F8D /* iOSLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSLifecycleMonitor.swift; sourceTree = "<group>"; };
150-
96259F8226CEF526008AE301 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
150+
96259F8226CEF526008AE301 /* LogTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogTarget.swift; sourceTree = "<group>"; };
151151
96259F8526CF1D45008AE301 /* ConsoleTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleTarget.swift; sourceTree = "<group>"; };
152152
96469AF72706225900AC5772 /* SystemTarget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SystemTarget.swift; sourceTree = "<group>"; };
153153
9679DD6226EFF00800A6933C /* ExampleDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleDestination.swift; sourceTree = "<group>"; };
154-
967C40D9258D472C008EB0B6 /* Logger_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger_Tests.swift; sourceTree = "<group>"; };
154+
967C40D9258D472C008EB0B6 /* SegmentLog_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLog_Tests.swift; sourceTree = "<group>"; };
155155
967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Metrics_Tests.swift; sourceTree = "<group>"; };
156156
967C40ED259A7311008EB0B6 /* HTTPClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClient.swift; sourceTree = "<group>"; };
157157
9692724D25A4E5B7009B5298 /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = "<group>"; };
158158
9692726725A583A6009B5298 /* SegmentDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentDestination.swift; sourceTree = "<group>"; };
159-
96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLogger.swift; sourceTree = "<group>"; };
160-
96C33AAB25892D6D00F3D538 /* Metrics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metrics.swift; sourceTree = "<group>"; };
159+
96C33A9B25880A5E00F3D538 /* SegmentLog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLog.swift; sourceTree = "<group>"; };
161160
96C33AB0258961F500F3D538 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
161+
96C95B15271DE22600C3EB9A /* LogTarget_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogTarget_Tests.swift; sourceTree = "<group>"; };
162162
96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline_Tests.swift; sourceTree = "<group>"; };
163163
A31A16252576B6F200C9CDDF /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = "<group>"; };
164164
A31A162E2576B73F00C9CDDF /* State.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = State.swift; sourceTree = "<group>"; };
@@ -285,7 +285,6 @@
285285
children = (
286286
96259F8426CEF534008AE301 /* Logger */,
287287
969A533D25B0D510009227D9 /* Platforms */,
288-
96C33AAB25892D6D00F3D538 /* Metrics.swift */,
289288
46A018C125E5857D00F9CCD8 /* Context.swift */,
290289
9692726725A583A6009B5298 /* SegmentDestination.swift */,
291290
46210835260BBEE400EBC4A8 /* DeviceToken.swift */,
@@ -297,8 +296,8 @@
297296
96259F8426CEF534008AE301 /* Logger */ = {
298297
isa = PBXGroup;
299298
children = (
300-
96259F8226CEF526008AE301 /* Logger.swift */,
301-
96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */,
299+
96259F8226CEF526008AE301 /* LogTarget.swift */,
300+
96C33A9B25880A5E00F3D538 /* SegmentLog.swift */,
302301
96259F8526CF1D45008AE301 /* ConsoleTarget.swift */,
303302
96469AF72706225900AC5772 /* SystemTarget.swift */,
304303
);
@@ -373,13 +372,14 @@
373372
4658175325BA4C20006B2809 /* HTTPClient_Tests.swift */,
374373
A31A16502576C47400C9CDDF /* JSON_Tests.swift */,
375374
46210810260538BE00EBC4A8 /* KeyPath_Tests.swift */,
376-
967C40D9258D472C008EB0B6 /* Logger_Tests.swift */,
375+
96C95B15271DE22600C3EB9A /* LogTarget_Tests.swift */,
377376
967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */,
378377
46F7485F26C720F60042798E /* ObjC_Tests.swift */,
378+
967C40D9258D472C008EB0B6 /* SegmentLog_Tests.swift */,
379379
46FE4D1C25A7A850003A7362 /* Storage_Tests.swift */,
380+
4621082D2609206D00EBC4A8 /* Support */,
380381
96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */,
381382
OBJ_13 /* XCTestManifests.swift */,
382-
4621082D2609206D00EBC4A8 /* Support */,
383383
);
384384
name = "Segment-Tests";
385385
path = "Tests/Segment-Tests";
@@ -554,21 +554,20 @@
554554
4663C729267A799100ADDD1A /* QueueTimer.swift in Sources */,
555555
46FE4C9C25A3F41C003A7362 /* LinuxLifecycleMonitor.swift in Sources */,
556556
460227422612987300A9E913 /* watchOSLifecycleEvents.swift in Sources */,
557-
96259F8326CEF526008AE301 /* Logger.swift in Sources */,
557+
96259F8326CEF526008AE301 /* LogTarget.swift in Sources */,
558558
46F7485E26C718710042798E /* ObjCConfiguration.swift in Sources */,
559559
A31A162F2576B73F00C9CDDF /* State.swift in Sources */,
560560
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */,
561561
4602276C261E7BF900A9E913 /* iOSDelegation.swift in Sources */,
562562
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */,
563-
96C33A9C25880A5E00F3D538 /* SegmentLogger.swift in Sources */,
563+
96C33A9C25880A5E00F3D538 /* SegmentLog.swift in Sources */,
564564
46FE4C9725A3F35E003A7362 /* macOSLifecycleMonitor.swift in Sources */,
565565
9620862C2575C0C800314F8D /* Events.swift in Sources */,
566566
A3AEE1882581A8F1002386EB /* Deprecations.swift in Sources */,
567567
966945D7259BDCDD00271339 /* HTTPClient.swift in Sources */,
568568
A31A16CA25794D9700C9CDDF /* Plugins.swift in Sources */,
569569
46A018C225E5857D00F9CCD8 /* Context.swift in Sources */,
570570
96208650257AA83E00314F8D /* iOSLifecycleMonitor.swift in Sources */,
571-
96C33AAC25892D6D00F3D538 /* Metrics.swift in Sources */,
572571
46031D65266E7C10009BA540 /* StartupQueue.swift in Sources */,
573572
465879BB2686560C00180335 /* watchOSLifecycleMonitor.swift in Sources */,
574573
);
@@ -585,10 +584,11 @@
585584
4658175425BA4C20006B2809 /* HTTPClient_Tests.swift in Sources */,
586585
46210811260538BE00EBC4A8 /* KeyPath_Tests.swift in Sources */,
587586
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */,
587+
96C95B16271DE22700C3EB9A /* LogTarget_Tests.swift in Sources */,
588588
A31A16512576C47400C9CDDF /* JSON_Tests.swift in Sources */,
589589
46FE4D1D25A7A850003A7362 /* Storage_Tests.swift in Sources */,
590590
46FE4CFB25A6C671003A7362 /* TestUtilities.swift in Sources */,
591-
967C40DA258D472C008EB0B6 /* Logger_Tests.swift in Sources */,
591+
967C40DA258D472C008EB0B6 /* SegmentLog_Tests.swift in Sources */,
592592
);
593593
runOnlyForDeploymentPostprocessing = 0;
594594
};
@@ -690,7 +690,7 @@
690690
"@loader_path/../Frameworks",
691691
"@loader_path/Frameworks",
692692
);
693-
MACOSX_DEPLOYMENT_TARGET = 10.15;
693+
MACOSX_DEPLOYMENT_TARGET = 11.0;
694694
OTHER_CFLAGS = "$(inherited)";
695695
OTHER_LDFLAGS = "$(inherited)";
696696
OTHER_SWIFT_FLAGS = "$(inherited)";
@@ -716,7 +716,7 @@
716716
"@loader_path/../Frameworks",
717717
"@loader_path/Frameworks",
718718
);
719-
MACOSX_DEPLOYMENT_TARGET = 10.15;
719+
MACOSX_DEPLOYMENT_TARGET = 11.0;
720720
OTHER_CFLAGS = "$(inherited)";
721721
OTHER_LDFLAGS = "$(inherited)";
722722
OTHER_SWIFT_FLAGS = "$(inherited)";

Sources/Segment/Plugins/Logger/Logger.swift renamed to Sources/Segment/Plugins/Logger/LogTarget.swift

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
2-
// Logger.swift
3-
// Logger
2+
// LogTarget.swift
3+
// LogTarget
44
//
55
// Created by Cody Garvin on 8/19/21.
66
//
@@ -80,10 +80,10 @@ public struct LoggingType: Hashable {
8080
}
8181
}
8282

83-
8483
/// The interface to the message being returned to `LogTarget` -> `parseLog()`.
8584
public protocol LogMessage {
8685
var kind: LogFilterKind { get }
86+
var title: String? { get }
8787
var message: String { get }
8888
var event: RawEvent? { get }
8989
var function: String? { get }
@@ -93,6 +93,29 @@ public protocol LogMessage {
9393
}
9494

9595

96+
public enum MetricType: Int {
97+
case counter = 0 // Not Verbose
98+
case gauge // Semi-verbose
99+
100+
func toString() -> String {
101+
var typeString = "Gauge"
102+
if self == .counter {
103+
typeString = "Counter"
104+
}
105+
return typeString
106+
}
107+
108+
static func fromString(_ string: String) -> Self {
109+
var returnType = Self.counter
110+
if string == "Gauge" {
111+
returnType = .gauge
112+
}
113+
114+
return returnType
115+
}
116+
}
117+
118+
96119
// MARK: - Public Logging API
97120

98121
extension Analytics {
@@ -107,20 +130,17 @@ extension Analytics {
107130
public func log(message: String, kind: LogFilterKind? = nil, function: String = #function, line: Int = #line) {
108131
apply { plugin in
109132
// Check if we should send off the event
110-
if Logger.loggingEnabled == false {
133+
if SegmentLog.loggingEnabled == false {
111134
return
112135
}
113-
if let loggerPlugin = plugin as? Logger {
136+
if let loggerPlugin = plugin as? SegmentLog {
114137
var filterKind = loggerPlugin.filterKind
115138
if let logKind = kind {
116139
filterKind = logKind
117140
}
118-
do {
119-
let log = try LogFactory.buildLog(destination: .log, title: "", message: message, kind: filterKind, function: function, line: line)
120-
loggerPlugin.log(log, destination: .log)
121-
} catch {
122-
segmentLog(message: "Could not build log: \(error.localizedDescription)", kind: .error)
123-
}
141+
142+
let log = LogFactory.buildLog(destination: .log, title: "", message: message, kind: filterKind, function: function, line: line)
143+
loggerPlugin.log(log, destination: .log)
124144
}
125145
}
126146
}
@@ -132,20 +152,17 @@ extension Analytics {
132152
/// - value: The value associated with the metric. This would be an incrementing counter or time
133153
/// or pressure gauge.
134154
/// - tags: Any tags that should be associated with the metric. Any extra metadata that may help.
135-
public func metric(_ type: String, name: String, value: Double, tags: [String]? = nil) {
155+
public func metric(_ type: MetricType, name: String, value: Double, tags: [String]? = nil) {
136156
apply { plugin in
137157
// Check if we should send off the event
138-
if Logger.loggingEnabled == false {
158+
if SegmentLog.loggingEnabled == false {
139159
return
140160
}
141161

142-
if let loggerPlugin = plugin as? Logger {
143-
do {
144-
let log = try LogFactory.buildLog(destination: .metric, title: type, message: name, value: value, tags: tags)
145-
loggerPlugin.log(log, destination: .metric)
146-
} catch {
147-
segmentLog(message: "Could not build metric: \(error.localizedDescription)", kind: .error)
148-
}
162+
if let loggerPlugin = plugin as? SegmentLog {
163+
164+
let log = LogFactory.buildLog(destination: .metric, title: type.toString(), message: name, value: value, tags: tags)
165+
loggerPlugin.log(log, destination: .metric)
149166
}
150167
}
151168
}
@@ -161,17 +178,13 @@ extension Analytics {
161178
public func history(event: RawEvent, sender: AnyObject, function: String = #function, line: Int = #line) {
162179
apply { plugin in
163180
// Check if we should send off the event
164-
if Logger.loggingEnabled == false {
181+
if SegmentLog.loggingEnabled == false {
165182
return
166183
}
167184

168-
if let loggerPlugin = plugin as? Logger {
169-
do {
170-
let log = try LogFactory.buildLog(destination: .history, title: event.toString(), message: "", function: function, line: line, event: event, sender: sender)
171-
loggerPlugin.log(log, destination: .metric)
172-
} catch {
173-
segmentLog(message: "Could not build history: \(error.localizedDescription)", kind: .error)
174-
}
185+
if let loggerPlugin = plugin as? SegmentLog {
186+
let log = LogFactory.buildLog(destination: .history, title: event.toString(), message: "", function: function, line: line, event: event, sender: sender)
187+
loggerPlugin.log(log, destination: .metric)
175188
}
176189
}
177190
}
@@ -185,21 +198,21 @@ extension Analytics {
185198
/// - target: A `LogTarget` that has logic to parse and handle log messages.
186199
/// - type: The type consists of `log`, `metric` or `history`. These correspond to the
187200
/// public API on Analytics.
188-
public func add(target: LogTarget, type: LoggingType) throws {
201+
public func add(target: LogTarget, type: LoggingType) {
189202
apply { (potentialLogger) in
190-
if let logger = potentialLogger as? Logger {
203+
if let logger = potentialLogger as? SegmentLog {
191204
do {
192205
try logger.add(target: target, for: type)
193206
} catch {
194-
log(message: "Could not add target: \(error.localizedDescription)", kind: .error)
207+
Self.segmentLog(message: "Could not add target: \(error.localizedDescription)", kind: .error)
195208
}
196209
}
197210
}
198211
}
199212

200213
public func logFlush() {
201214
apply { (potentialLogger) in
202-
if let logger = potentialLogger as? Logger {
215+
if let logger = potentialLogger as? SegmentLog {
203216
logger.flush()
204217
}
205218
}

0 commit comments

Comments
 (0)