Skip to content

Commit 4a103d0

Browse files
authored
Added Logger and SegmentLogger plugin. (#46)
* Added new console target and replaced all prints * Added date and os_log * Added flag to disable logging
1 parent d28a2c9 commit 4a103d0

File tree

14 files changed

+552
-105
lines changed

14 files changed

+552
-105
lines changed

Examples/apps/DestinationsExample/DestinationsExample/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ViewController: UIViewController {
8484
case .alias:
8585
aliasEvent()
8686
case .none:
87-
print("Failed to establish event type")
87+
analytics?.log(message: "Failed to establish event type", kind: .error)
8888
}
8989

9090
clearAll()

Examples/apps/SegmentExtensionsExample/SegmentExtensionsExample/LoginViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extension LoginViewController: ASAuthorizationControllerDelegate {
107107
analytics?.track(name: "Saved to Keychain")
108108
} catch {
109109
//handle error and optionally track it
110-
print("Unable to save userId to keychain.")
110+
analytics?.log(message: "Unable to save userId to keychain.", kind: .error)
111111
}
112112
}
113113

@@ -140,7 +140,7 @@ extension LoginViewController: ASAuthorizationControllerDelegate {
140140

141141
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
142142
//handle error
143-
print(error)
143+
analytics?.log(message: error, king: .error)
144144
}
145145
}
146146

Examples/apps/SegmentExtensionsExample/Supporting Files/KeychainItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct KeychainItem {
143143
do {
144144
try KeychainItem(service: "co.alancharles.SegmentExtensionsExample", account: "userIdentifier").deleteItem()
145145
} catch {
146-
print("Unable to delete userIdentifier from keychain")
146+
analytics?.log(message: "Unable to delete userIdentifier from keychain", kind: .error)
147147
}
148148
}
149149
}

Examples/destination_plugins/AppsFlyerDestination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ extension AppsFlyerDestination: AppsFlyerLibDelegate {
254254
extension AppsFlyerDestination: DeepLinkDelegate, UIApplicationDelegate {
255255

256256
func didResolveDeepLink(_ result: DeepLinkResult) {
257-
print(result)
257+
analytics?.log(message: "AppsFlyer Deeplink Result: \(result)")
258258
switch result.status {
259259
case .notFound:
260260
analytics?.log(message: "AppsFlyer: Deep link not found")

Examples/other_plugins/ConsoleLogger.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ class ConsoleLogger: Plugin {
5353
// we want to log every event, so lets override `execute`.
5454
func execute<T: RawEvent>(event: T?) -> T? {
5555
if let json = event?.prettyPrint() {
56-
print("event received on instance: \(name)")
57-
print("\(json)\n")
56+
analytics?.log(message: "event received on instance: \(name)")
57+
analytics?.log(message: "\(json)\n")
5858
}
5959
return event
6060
}
6161

6262
// we also want to know when settings are retrieved or changed.
6363
func update(settings: Settings) {
6464
let json = settings.prettyPrint()
65-
print("settings updated on instance: \(name)")
66-
print("\(json)\n")
65+
analytics?.log(message: "settings updated on instance: \(name)\nPayload: \(json)")
6766
}
6867

6968
}

Examples/other_plugins/UIKitScreenTracking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ extension UIViewController {
122122
guard let top = Self.seg__visibleViewController(activeController()) else { return }
123123

124124
var name = String(describing: top.self.classForCoder).replacingOccurrences(of: "ViewController", with: "")
125-
print(name)
125+
analytics?.log(message: "Auto-tracking Screen: \(name)")
126126
// name could've been just "ViewController"...
127127
if name.count == 0 {
128128
name = top.title ?? "Unknown"

Segment.xcodeproj/project.pbxproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
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 */; };
53+
96259F8626CF1D45008AE301 /* ConsoleTarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96259F8526CF1D45008AE301 /* ConsoleTarget.swift */; };
5254
966945D7259BDCDD00271339 /* HTTPClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40ED259A7311008EB0B6 /* HTTPClient.swift */; };
5355
967C40DA258D472C008EB0B6 /* Logger_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40D9258D472C008EB0B6 /* Logger_Tests.swift */; };
5456
967C40E3258D4DAF008EB0B6 /* Metrics_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */; };
5557
9692724E25A4E5B7009B5298 /* Startup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692724D25A4E5B7009B5298 /* Startup.swift */; };
5658
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9692726725A583A6009B5298 /* SegmentDestination.swift */; };
57-
96C33A9C25880A5E00F3D538 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* Logger.swift */; };
59+
96C33A9C25880A5E00F3D538 /* SegmentLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */; };
5860
96C33AAC25892D6D00F3D538 /* Metrics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AAB25892D6D00F3D538 /* Metrics.swift */; };
5961
96C33AB1258961F500F3D538 /* Settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C33AB0258961F500F3D538 /* Settings.swift */; };
6062
96DBF37B26F39B5500724B0B /* Timeline_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */; };
@@ -144,13 +146,15 @@
144146
9620862B2575C0C800314F8D /* Events.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Events.swift; sourceTree = "<group>"; };
145147
962086482579CCC200314F8D /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
146148
9620864F257AA83E00314F8D /* iOSLifecycleMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSLifecycleMonitor.swift; sourceTree = "<group>"; };
149+
96259F8226CEF526008AE301 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
150+
96259F8526CF1D45008AE301 /* ConsoleTarget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleTarget.swift; sourceTree = "<group>"; };
147151
9679DD6226EFF00800A6933C /* ExampleDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleDestination.swift; sourceTree = "<group>"; };
148152
967C40D9258D472C008EB0B6 /* Logger_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger_Tests.swift; sourceTree = "<group>"; };
149153
967C40E2258D4DAF008EB0B6 /* Metrics_Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Metrics_Tests.swift; sourceTree = "<group>"; };
150154
967C40ED259A7311008EB0B6 /* HTTPClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPClient.swift; sourceTree = "<group>"; };
151155
9692724D25A4E5B7009B5298 /* Startup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Startup.swift; sourceTree = "<group>"; };
152156
9692726725A583A6009B5298 /* SegmentDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentDestination.swift; sourceTree = "<group>"; };
153-
96C33A9B25880A5E00F3D538 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
157+
96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentLogger.swift; sourceTree = "<group>"; };
154158
96C33AAB25892D6D00F3D538 /* Metrics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metrics.swift; sourceTree = "<group>"; };
155159
96C33AB0258961F500F3D538 /* Settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Settings.swift; sourceTree = "<group>"; };
156160
96DBF37A26F39B5500724B0B /* Timeline_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline_Tests.swift; sourceTree = "<group>"; };
@@ -277,8 +281,8 @@
277281
9620864E257AA82900314F8D /* Plugins */ = {
278282
isa = PBXGroup;
279283
children = (
284+
96259F8426CEF534008AE301 /* Logger */,
280285
969A533D25B0D510009227D9 /* Platforms */,
281-
96C33A9B25880A5E00F3D538 /* Logger.swift */,
282286
96C33AAB25892D6D00F3D538 /* Metrics.swift */,
283287
46A018C125E5857D00F9CCD8 /* Context.swift */,
284288
9692726725A583A6009B5298 /* SegmentDestination.swift */,
@@ -288,6 +292,16 @@
288292
path = Plugins;
289293
sourceTree = "<group>";
290294
};
295+
96259F8426CEF534008AE301 /* Logger */ = {
296+
isa = PBXGroup;
297+
children = (
298+
96259F8226CEF526008AE301 /* Logger.swift */,
299+
96C33A9B25880A5E00F3D538 /* SegmentLogger.swift */,
300+
96259F8526CF1D45008AE301 /* ConsoleTarget.swift */,
301+
);
302+
path = Logger;
303+
sourceTree = "<group>";
304+
};
291305
969A533D25B0D510009227D9 /* Platforms */ = {
292306
isa = PBXGroup;
293307
children = (
@@ -521,6 +535,7 @@
521535
4621080C2605332D00EBC4A8 /* KeyPath.swift in Sources */,
522536
A31A16262576B6F200C9CDDF /* Timeline.swift in Sources */,
523537
96C33AB1258961F500F3D538 /* Settings.swift in Sources */,
538+
96259F8626CF1D45008AE301 /* ConsoleTarget.swift in Sources */,
524539
46E382E72654429A00BA2502 /* Utils.swift in Sources */,
525540
A31A16B225781CB400C9CDDF /* JSON.swift in Sources */,
526541
46022771261F7A4800A9E913 /* Atomic.swift in Sources */,
@@ -535,12 +550,13 @@
535550
4663C729267A799100ADDD1A /* QueueTimer.swift in Sources */,
536551
46FE4C9C25A3F41C003A7362 /* LinuxLifecycleMonitor.swift in Sources */,
537552
460227422612987300A9E913 /* watchOSLifecycleEvents.swift in Sources */,
553+
96259F8326CEF526008AE301 /* Logger.swift in Sources */,
538554
46F7485E26C718710042798E /* ObjCConfiguration.swift in Sources */,
539555
A31A162F2576B73F00C9CDDF /* State.swift in Sources */,
540556
9692726825A583A6009B5298 /* SegmentDestination.swift in Sources */,
541557
4602276C261E7BF900A9E913 /* iOSDelegation.swift in Sources */,
542558
46A018D425E6C9C200F9CCD8 /* LinuxUtils.swift in Sources */,
543-
96C33A9C25880A5E00F3D538 /* Logger.swift in Sources */,
559+
96C33A9C25880A5E00F3D538 /* SegmentLogger.swift in Sources */,
544560
46FE4C9725A3F35E003A7362 /* macOSLifecycleMonitor.swift in Sources */,
545561
9620862C2575C0C800314F8D /* Events.swift in Sources */,
546562
A3AEE1882581A8F1002386EB /* Deprecations.swift in Sources */,

Sources/Segment/Plugins/Logger.swift

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ConsoleTarget.swift
3+
// ConsoleTarget
4+
//
5+
// Created by Cody Garvin on 8/19/21.
6+
//
7+
8+
import Foundation
9+
10+
class ConsoleTarget: LogTarget {
11+
func parseLog(_ log: LogMessage) {
12+
var metadata = ""
13+
if let function = log.function, let line = log.line {
14+
metadata = " - \(function):\(line)"
15+
}
16+
print("[Segment \(log.kind.toString())\(metadata)]\n\(log.message)\n")
17+
}
18+
}

0 commit comments

Comments
 (0)