From f54bbd54b779b910e47065dcc72c113972d2fdc1 Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Wed, 5 Jan 2022 17:48:19 +0000 Subject: [PATCH 1/7] Add the sending of timestamp from the device logs --- Example/Podfile.lock | 2 +- JustLog/Classes/Logger.swift | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 9a731c8..933221b 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -15,7 +15,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - JustLog: db54ca372b659e3da001d24f6e3c701ed552d87e + JustLog: ea1ac3376f8f5ae785cc3f37f3a3f258f1354d54 SwiftyBeaver: 84069991dd5dca07d7069100985badaca7f0ce82 PODFILE CHECKSUM: c7680d001abb03143de15e13f1b6fbabb258d898 diff --git a/JustLog/Classes/Logger.swift b/JustLog/Classes/Logger.swift index a635a4d..6f3db60 100644 --- a/JustLog/Classes/Logger.swift +++ b/JustLog/Classes/Logger.swift @@ -66,7 +66,8 @@ public final class Logger: NSObject { public var iosVersionKey = "ios_version" public var deviceTypeKey = "ios_device" public var appBundleID = "app_bundle_ID" - + public var deviceTimestampKey = "device_timestamp" + public var errorDomain = "error_domain" public var errorCode = "error_code" @@ -264,7 +265,7 @@ extension Logger: Logging { extension Logger { - internal func logMessage(_ message: String, error: NSError?, userInfo: [String : Any]?, _ file: String, _ function: String, _ line: UInt) -> String { + internal func logMessage(_ message: String, error: NSError?, userInfo: [String : Any]?, currentDate: Date = Date(), _ file: String, _ function: String, _ line: UInt) -> String { let messageConst = "message" let userInfoConst = "user_info" @@ -275,7 +276,7 @@ extension Logger { var retVal = [String : Any]() retVal[messageConst] = message - retVal[metadataConst] = metadataDictionary(file, function, line) + retVal[metadataConst] = metadataDictionary(file, function, line, currentDate) if let userInfo = userInfo { for (key, value) in userInfo { @@ -294,7 +295,7 @@ extension Logger { return retVal.toJSON() ?? "" } - private func metadataDictionary(_ file: String, _ function: String, _ line: UInt) -> [String: Any] { + private func metadataDictionary(_ file: String, _ function: String, _ line: UInt, _ currentDate: Date) -> [String: Any] { var fileMetadata = [String : String]() if let url = URL(string: file) { @@ -311,6 +312,7 @@ extension Logger { fileMetadata[iosVersionKey] = UIDevice.current.systemVersion fileMetadata[deviceTypeKey] = UIDevice.current.platform() fileMetadata[appBundleID] = Bundle.main.bundleIdentifier + fileMetadata[deviceTimestampKey] = "\(currentDate.timeIntervalSince1970)" return fileMetadata } From bce25fe7b4eb601c9531edcbaa2e5f34526fa583 Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Wed, 5 Jan 2022 17:48:46 +0000 Subject: [PATCH 2/7] Updated unit tests --- Example/Tests/CustomDestinationTests.swift | 25 +++++++++++++++++++++- Example/Tests/LoggerTests.swift | 25 +++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Example/Tests/CustomDestinationTests.swift b/Example/Tests/CustomDestinationTests.swift index 7d87517..84ec234 100644 --- a/Example/Tests/CustomDestinationTests.swift +++ b/Example/Tests/CustomDestinationTests.swift @@ -11,12 +11,16 @@ import XCTest class MockCustomDestinationSender: CustomDestinationSender { let expectation: XCTestExpectation + var logs: [String] + init(expectation: XCTestExpectation) { self.expectation = expectation + self.logs = [] } func log(_ string: String) { - self.expectation.fulfill() + expectation.fulfill() + logs.append(string) } } @@ -37,5 +41,24 @@ class CustomDestinationTests: XCTestCase { } self.waitForExpectations(timeout: 10.0, handler: nil) } + + func test_logger_sendsDeviceTimestampForEachLogType() { + let sut = Logger.shared + sut.enableCustomLogging = true + + let expectation = expectation(description: #function) + expectation.expectedFulfillmentCount = 5 + let mockSender = MockCustomDestinationSender(expectation: expectation) + sut.setupWithCustomLogSender(mockSender) + + sut.verbose("Verbose Message", error: nil, userInfo: nil, #file, #function, #line) + sut.debug("Debug Message", error: nil, userInfo: nil, #file, #function, #line) + sut.info("Info Message", error: nil, userInfo: nil, #file, #function, #line) + sut.warning("Warning Message", error: nil, userInfo: nil, #file, #function, #line) + sut.error("Error Message", error: nil, userInfo: nil, #file, #function, #line) + + mockSender.logs.forEach { XCTAssertTrue($0.contains("device_timestamp")) } + self.waitForExpectations(timeout: 10.0, handler: nil) + } } diff --git a/Example/Tests/LoggerTests.swift b/Example/Tests/LoggerTests.swift index 03bdb7b..e8eee98 100644 --- a/Example/Tests/LoggerTests.swift +++ b/Example/Tests/LoggerTests.swift @@ -98,15 +98,34 @@ class LoggerTests: XCTestCase { XCTAssertTrue(sut.queuedLogs.isEmpty) } -func test_logger_whenLogMessagesAreSanitized_thenExpectedResultRetrived() { + func test_logger_whenLogMessagesAreSanitized_thenExpectedResultRetrived() { let sut = Logger.shared sut.setup() var message = "conversation = {name = \\\"John Smith\\\";\\n; \\n token = \\\"123453423\\\";\\n" let expectedMessage = "conversation = {n***e = \\\"*****\\\";\\n; \\n t***n = \\\"*****\\\";\\n" - + message = sut.sanitize(message, Logger.LogType.error) sut.error(message, error: nil, userInfo: nil, #file, #function, #line) - + XCTAssertEqual(message, expectedMessage) } + + func test_logger_sendsDeviceTimestampAsMetadata() throws { + let sut = Logger.shared + + let currentDate = Date() + let message = sut.logMessage("Log message", error: nil, userInfo: nil, currentDate: currentDate, #file, #function, #line) + + let data = try XCTUnwrap(message.data(using: .utf8)) + guard let parsedMessage = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [String: Any] else { + XCTFail("Failed to parse log message") + return + } + + let parsedMetadata = try XCTUnwrap(parsedMessage["metadata"] as? [String: String]) + let parsedDeviceTimestamp = try XCTUnwrap(parsedMetadata["device_timestamp"]) + let expectedDeviceTimestamp = "\(currentDate.timeIntervalSince1970)" + + XCTAssertEqual(parsedDeviceTimestamp, expectedDeviceTimestamp) + } } From 2112a96f45240bd72c3a950b1ce8f6d6111875b8 Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Thu, 6 Jan 2022 09:49:42 +0000 Subject: [PATCH 3/7] Test cleanup --- Example/Tests/LoggerTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example/Tests/LoggerTests.swift b/Example/Tests/LoggerTests.swift index e8eee98..224063f 100644 --- a/Example/Tests/LoggerTests.swift +++ b/Example/Tests/LoggerTests.swift @@ -117,7 +117,7 @@ class LoggerTests: XCTestCase { let message = sut.logMessage("Log message", error: nil, userInfo: nil, currentDate: currentDate, #file, #function, #line) let data = try XCTUnwrap(message.data(using: .utf8)) - guard let parsedMessage = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as? [String: Any] else { + guard let parsedMessage = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { XCTFail("Failed to parse log message") return } From 12f3757c8aceb0d0614e3f9d371e00e0b0e241e8 Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Thu, 6 Jan 2022 09:49:52 +0000 Subject: [PATCH 4/7] Bumped module version --- Example/Podfile.lock | 4 ++-- JustLog.podspec | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 933221b..618fd12 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - JustLog (3.7.2): + - JustLog (3.7.3): - SwiftyBeaver (~> 1.9.3) - SwiftyBeaver (1.9.5) @@ -15,7 +15,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - JustLog: ea1ac3376f8f5ae785cc3f37f3a3f258f1354d54 + JustLog: b6ac20eb81bf9a542c6ee4f87b78a3a4d8dd7815 SwiftyBeaver: 84069991dd5dca07d7069100985badaca7f0ce82 PODFILE CHECKSUM: c7680d001abb03143de15e13f1b6fbabb258d898 diff --git a/JustLog.podspec b/JustLog.podspec index ce98d4c..9e0b9ba 100644 --- a/JustLog.podspec +++ b/JustLog.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'JustLog' - s.version = '3.7.2' + s.version = '3.7.3' s.summary = 'JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort.' s.description = "<<-DESC From a5dc6689d304ea20179ee3057d7639558455903d Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Thu, 6 Jan 2022 17:16:46 +0000 Subject: [PATCH 5/7] Updated PR workflow to use Xcode 13.2.1 --- .github/workflows/pull-request-workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull-request-workflow.yml b/.github/workflows/pull-request-workflow.yml index b05e83c..7d19a9a 100644 --- a/.github/workflows/pull-request-workflow.yml +++ b/.github/workflows/pull-request-workflow.yml @@ -16,7 +16,7 @@ jobs: - name: Setup Xcode uses: maxim-lobanov/setup-xcode@v1 with: - xcode-version: '12.4' + xcode-version: '13.2.1' - name: Setup ruby and bundler dependencies uses: ruby/setup-ruby@v1.81.0 with: From 96fabd0d53104562c6c570fa8dea07abae560a8d Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Thu, 6 Jan 2022 17:27:48 +0000 Subject: [PATCH 6/7] Bumped version to 3.8.0 --- JustLog.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JustLog.podspec b/JustLog.podspec index 9e0b9ba..732ebe5 100644 --- a/JustLog.podspec +++ b/JustLog.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'JustLog' - s.version = '3.7.3' + s.version = '3.8.0' s.summary = 'JustLog brings logging on iOS to the next level. It supports console, file and remote Logstash logging via TCP socket with no effort.' s.description = "<<-DESC From 50e308c4a18ef5be6f420aeb801715917a8a1b63 Mon Sep 17 00:00:00 2001 From: Victor Sarda Date: Fri, 7 Jan 2022 09:16:37 +0000 Subject: [PATCH 7/7] Updated Podfile.lock in the Example app --- Example/Podfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 618fd12..f46ec49 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - JustLog (3.7.3): + - JustLog (3.8.0): - SwiftyBeaver (~> 1.9.3) - SwiftyBeaver (1.9.5) @@ -15,7 +15,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - JustLog: b6ac20eb81bf9a542c6ee4f87b78a3a4d8dd7815 + JustLog: c4636ea332cf2c677bde5d58d67347880c6cc3e2 SwiftyBeaver: 84069991dd5dca07d7069100985badaca7f0ce82 PODFILE CHECKSUM: c7680d001abb03143de15e13f1b6fbabb258d898