Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose publicly some types and methods related to the logging system #626

Merged
merged 9 commits into from
Jan 31, 2025
2 changes: 1 addition & 1 deletion Mixpanel-swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Pod::Spec.new do |s|
}
s.default_subspec = 'Complete'
base_source_files = ['Sources/Network.swift', 'Sources/FlushRequest.swift', 'Sources/PrintLogging.swift', 'Sources/FileLogging.swift',
'Sources/Logger.swift', 'Sources/JSONHandler.swift', 'Sources/Error.swift', 'Sources/AutomaticProperties.swift',
'Sources/MixpanelLogger.swift', 'Sources/JSONHandler.swift', 'Sources/Error.swift', 'Sources/AutomaticProperties.swift',
'Sources/Constants.swift', 'Sources/MixpanelType.swift', 'Sources/Mixpanel.swift', 'Sources/MixpanelInstance.swift',
'Sources/Flush.swift','Sources/Track.swift', 'Sources/People.swift', 'Sources/AutomaticEvents.swift',
'Sources/Group.swift',
Expand Down
20 changes: 10 additions & 10 deletions Mixpanel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
17C6547B2BB1F16000C8A126 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 1728208D2BA8BDE4002CD973 /* PrivacyInfo.xcprivacy */; };
17C6547C2BB1F16400C8A126 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 1728208D2BA8BDE4002CD973 /* PrivacyInfo.xcprivacy */; };
17C6547D2BB1F16800C8A126 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 1728208D2BA8BDE4002CD973 /* PrivacyInfo.xcprivacy */; };
51DD567C1D306B740045D3DB /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* Logger.swift */; };
51DD567C1D306B740045D3DB /* MixpanelLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* MixpanelLogger.swift */; };
51DD56831D306B7B0045D3DB /* PrintLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56801D306B7B0045D3DB /* PrintLogging.swift */; };
51DD56841D306B7B0045D3DB /* FileLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56811D306B7B0045D3DB /* FileLogging.swift */; };
673ABE3A21360CBE00B1784B /* Group.swift in Sources */ = {isa = PBXBuildFile; fileRef = 673ABE3921360CBE00B1784B /* Group.swift */; };
Expand All @@ -28,7 +28,7 @@
86F86EB7224439D300B69832 /* Mixpanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115948A1CFF1538007F8B4F /* Mixpanel.swift */; };
86F86EB8224439D300B69832 /* MixpanelInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115948D1D000709007F8B4F /* MixpanelInstance.swift */; };
86F86EB9224439DC00B69832 /* AutomaticProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1D335CF1D3059A800E68E12 /* AutomaticProperties.swift */; };
86F86EBA224439E300B69832 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* Logger.swift */; };
86F86EBA224439E300B69832 /* MixpanelLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* MixpanelLogger.swift */; };
86F86EBB224439EB00B69832 /* FlushRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1D335CB1D303A0D00E68E12 /* FlushRequest.swift */; };
86F86EBC224439F100B69832 /* PrintLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56801D306B7B0045D3DB /* PrintLogging.swift */; };
86F86EBD224439F500B69832 /* Flush.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115949E1D01BE14007F8B4F /* Flush.swift */; };
Expand All @@ -55,7 +55,7 @@
E11594A11D01C597007F8B4F /* Track.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11594A01D01C597007F8B4F /* Track.swift */; };
E12782BB1D4AB5CB0025FB05 /* PrintLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56801D306B7B0045D3DB /* PrintLogging.swift */; };
E12782BC1D4AB5CB0025FB05 /* FileLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56811D306B7B0045D3DB /* FileLogging.swift */; };
E12782BD1D4AB5CB0025FB05 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* Logger.swift */; };
E12782BD1D4AB5CB0025FB05 /* MixpanelLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* MixpanelLogger.swift */; };
E12782BE1D4AB5CB0025FB05 /* Mixpanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115948A1CFF1538007F8B4F /* Mixpanel.swift */; };
E12782BF1D4AB5CB0025FB05 /* MixpanelInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = E115948D1D000709007F8B4F /* MixpanelInstance.swift */; };
E12782C11D4AB5CB0025FB05 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11594961D006022007F8B4F /* Network.swift */; };
Expand Down Expand Up @@ -85,7 +85,7 @@
E1F15FD61E64B5FC00391AE3 /* FlushRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1D335CB1D303A0D00E68E12 /* FlushRequest.swift */; };
E1F15FD71E64B60200391AE3 /* PrintLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56801D306B7B0045D3DB /* PrintLogging.swift */; };
E1F15FD81E64B60200391AE3 /* FileLogging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56811D306B7B0045D3DB /* FileLogging.swift */; };
E1F15FD91E64B60600391AE3 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* Logger.swift */; };
E1F15FD91E64B60600391AE3 /* MixpanelLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DD56791D306B740045D3DB /* MixpanelLogger.swift */; };
E1F15FDA1E64B60A00391AE3 /* JSONHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E11594981D01689F007F8B4F /* JSONHandler.swift */; };
E1F15FDB1E64B60A00391AE3 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1982BFE1D0AC2E2006B7330 /* Error.swift */; };
E1F15FDC1E64B60A00391AE3 /* AutomaticProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1D335CF1D3059A800E68E12 /* AutomaticProperties.swift */; };
Expand All @@ -100,7 +100,7 @@

/* Begin PBXFileReference section */
1728208D2BA8BDE4002CD973 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = Sources/Mixpanel/PrivacyInfo.xcprivacy; sourceTree = SOURCE_ROOT; };
51DD56791D306B740045D3DB /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
51DD56791D306B740045D3DB /* MixpanelLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MixpanelLogger.swift; sourceTree = "<group>"; };
51DD56801D306B7B0045D3DB /* PrintLogging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrintLogging.swift; sourceTree = "<group>"; };
51DD56811D306B7B0045D3DB /* FileLogging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileLogging.swift; sourceTree = "<group>"; };
673ABE3921360CBE00B1784B /* Group.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Group.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -174,7 +174,7 @@
isa = PBXGroup;
children = (
51DD56781D306B6B0045D3DB /* Logging */,
51DD56791D306B740045D3DB /* Logger.swift */,
51DD56791D306B740045D3DB /* MixpanelLogger.swift */,
);
name = Log;
sourceTree = "<group>";
Expand Down Expand Up @@ -492,7 +492,7 @@
86F86EBC224439F100B69832 /* PrintLogging.swift in Sources */,
868550AF2699096F001FCDDC /* MixpanelPersistence.swift in Sources */,
86F86EBB224439EB00B69832 /* FlushRequest.swift in Sources */,
86F86EBA224439E300B69832 /* Logger.swift in Sources */,
86F86EBA224439E300B69832 /* MixpanelLogger.swift in Sources */,
86F86EB9224439DC00B69832 /* AutomaticProperties.swift in Sources */,
86F86F3722497F2900B69832 /* AutomaticEvents.swift in Sources */,
86F86EB7224439D300B69832 /* Mixpanel.swift in Sources */,
Expand All @@ -515,7 +515,7 @@
E11594A11D01C597007F8B4F /* Track.swift in Sources */,
E11594991D01689F007F8B4F /* JSONHandler.swift in Sources */,
E1D335D01D3059A800E68E12 /* AutomaticProperties.swift in Sources */,
51DD567C1D306B740045D3DB /* Logger.swift in Sources */,
51DD567C1D306B740045D3DB /* MixpanelLogger.swift in Sources */,
E165228F1D6781DF000D5949 /* MixpanelType.swift in Sources */,
BB9614171F3BB87700C3EF3E /* ReadWriteLock.swift in Sources */,
E190522D1F9FC1BC00900E5D /* SessionMetadata.swift in Sources */,
Expand All @@ -535,7 +535,7 @@
67FF65E521878416005161FA /* Group.swift in Sources */,
E12782BB1D4AB5CB0025FB05 /* PrintLogging.swift in Sources */,
E12782BC1D4AB5CB0025FB05 /* FileLogging.swift in Sources */,
E12782BD1D4AB5CB0025FB05 /* Logger.swift in Sources */,
E12782BD1D4AB5CB0025FB05 /* MixpanelLogger.swift in Sources */,
E12782BE1D4AB5CB0025FB05 /* Mixpanel.swift in Sources */,
E12782BF1D4AB5CB0025FB05 /* MixpanelInstance.swift in Sources */,
E12782C11D4AB5CB0025FB05 /* Network.swift in Sources */,
Expand Down Expand Up @@ -564,7 +564,7 @@
E1F15FE01E64B60D00391AE3 /* MixpanelInstance.swift in Sources */,
E1F15FDF1E64B60D00391AE3 /* Mixpanel.swift in Sources */,
E1F15FDC1E64B60A00391AE3 /* AutomaticProperties.swift in Sources */,
E1F15FD91E64B60600391AE3 /* Logger.swift in Sources */,
E1F15FD91E64B60600391AE3 /* MixpanelLogger.swift in Sources */,
E1F15FD61E64B5FC00391AE3 /* FlushRequest.swift in Sources */,
E1F15FD71E64B60200391AE3 /* PrintLogging.swift in Sources */,
8625BEBD26D045CE0009BAA9 /* MPDB.swift in Sources */,
Expand Down
52 changes: 26 additions & 26 deletions MixpanelDemo/MixpanelDemoTests/LoggerTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// LoggerTests.swift
// MixpanelLoggerTests.swift
// MixpanelDemo
//
// Created by Sam Green on 7/8/16.
Expand All @@ -10,68 +10,68 @@ import Foundation
import XCTest
@testable import Mixpanel

class LoggerTests: XCTestCase {
class MixpanelLoggerTests: XCTestCase {


func testEnableDebug() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.debug)
MixpanelLogger.addLogging(counter)
MixpanelLogger.enableLevel(.debug)

Logger.debug(message: "logged")
MixpanelLogger.debug(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableInfo() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.info)
Logger.info(message: "logged")
MixpanelLogger.addLogging(counter)
MixpanelLogger.enableLevel(.info)
MixpanelLogger.info(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableWarning() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.warning)
Logger.warn(message: "logged")
MixpanelLogger.addLogging(counter)
MixpanelLogger.enableLevel(.warning)
MixpanelLogger.warn(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testEnableError() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.enableLevel(.error)
Logger.error(message: "logged")
MixpanelLogger.addLogging(counter)
MixpanelLogger.enableLevel(.error)
MixpanelLogger.error(message: "logged")
XCTAssertEqual(1, counter.count)
}

func testDisabledLogging() {
let counter = CounterLogging()
Logger.addLogging(counter)
Logger.disableLevel(.debug)
Logger.debug(message: "not logged")
MixpanelLogger.addLogging(counter)
MixpanelLogger.disableLevel(.debug)
MixpanelLogger.debug(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.error)
Logger.error(message: "not logged")
MixpanelLogger.disableLevel(.error)
MixpanelLogger.error(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.info)
Logger.info(message: "not logged")
MixpanelLogger.disableLevel(.info)
MixpanelLogger.info(message: "not logged")
XCTAssertEqual(0, counter.count)

Logger.disableLevel(.warning)
Logger.warn(message: "not logged")
MixpanelLogger.disableLevel(.warning)
MixpanelLogger.warn(message: "not logged")
XCTAssertEqual(0, counter.count)
}
}

/// This is a stub that implements `Logging` to be passed to our `Logger` instance for testing
class CounterLogging: Logging {
/// This is a stub that implements `MixpanelLogging` to be passed to our `MixpanelLogger` instance for testing
class CounterLogging: MixpanelLogging {
var count = 0

func addMessage(message: LogMessage) {
func addMessage(message: MixpanelLogMessage) {
count = count + 1
}
}
2 changes: 1 addition & 1 deletion Sources/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ErrorHandler {

class func logError(_ error: Error) {
let stackSymbols = Thread.callStackSymbols
Logger.error(message: "Error: \(error) \n Stack Symbols: \(stackSymbols)")
MixpanelLogger.error(message: "Error: \(error) \n Stack Symbols: \(stackSymbols)")
}

}
4 changes: 2 additions & 2 deletions Sources/FileLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// Logs all messages to a file
class FileLogging: Logging {
class FileLogging: MixpanelLogging {
private let fileHandle: FileHandle

init(path: String) {
Expand All @@ -28,7 +28,7 @@ class FileLogging: Logging {
fileHandle.closeFile()
}

func addMessage(message: LogMessage) {
func addMessage(message: MixpanelLogMessage) {
let string = "File: \(message.file) - Func: \(message.function) - " +
"Level: \(message.level.rawValue) - Message: \(message.text)"
if let data = string.data(using: String.Encoding.utf8) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class Flush: AppLifecycle {
(entity["id"] as? Int32) ?? 0
}
// Log data payload sent
Logger.debug(message: "Sending batch of data")
Logger.debug(message: batch as Any)
MixpanelLogger.debug(message: "Sending batch of data")
MixpanelLogger.debug(message: batch as Any)
let requestData = JSONHandler.encodeAPIData(batch)
if let requestData = requestData {
#if os(iOS)
Expand Down
4 changes: 2 additions & 2 deletions Sources/FlushRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ class FlushRequest: Network {
failure: { (reason, _, response) in
self.networkConsecutiveFailures += 1
self.updateRetryDelay(response)
Logger.warn(message: "API request to \(resource.path) has failed with reason \(reason)")
MixpanelLogger.warn(message: "API request to \(resource.path) has failed with reason \(reason)")
completion(false)
}, success: { (result, response) in
self.networkConsecutiveFailures = 0
self.updateRetryDelay(response)
if result == 0 {
Logger.info(message: "\(base) api rejected some items")
MixpanelLogger.info(message: "\(base) api rejected some items")
}
completion(true)
})
Expand Down
10 changes: 5 additions & 5 deletions Sources/JSONHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JSONHandler {
let data: Data? = serializeJSONObject(obj)

guard let d = data else {
Logger.warn(message: "couldn't serialize object")
MixpanelLogger.warn(message: "couldn't serialize object")
return nil
}

Expand All @@ -28,7 +28,7 @@ class JSONHandler {
do {
object = try JSONSerialization.jsonObject(with: data, options: [])
} catch {
Logger.warn(message: "exception decoding object data")
MixpanelLogger.warn(message: "exception decoding object data")
}
return object
}
Expand All @@ -44,7 +44,7 @@ class JSONHandler {
}

guard JSONSerialization.isValidJSONObject(serializableJSONObject) else {
Logger.warn(message: "object isn't valid and can't be serialzed to JSON")
MixpanelLogger.warn(message: "object isn't valid and can't be serialzed to JSON")
return nil
}

Expand All @@ -53,7 +53,7 @@ class JSONHandler {
serializedObject = try JSONSerialization
.data(withJSONObject: serializableJSONObject, options: [])
} catch {
Logger.warn(message: "exception encoding api data")
MixpanelLogger.warn(message: "exception encoding api data")
}
return serializedObject
}
Expand Down Expand Up @@ -110,7 +110,7 @@ class JSONHandler {
// all nil properties outside of Arrays are converted to NSNull()
return NSNull()
} else {
Logger.info(message: "enforcing string on object")
MixpanelLogger.info(message: "enforcing string on object")
return objString
}
}
Expand Down
Loading