Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #118 from victor-sarda/feature/vs/add-the-sending-…
Browse files Browse the repository at this point in the history
…of-timestamp-from-the-device-logs

Add the sending of timestamp from the device logs
  • Loading branch information
albertodebortoli authored Jan 10, 2022
2 parents d9271bd + 50e308c commit d96562f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- JustLog (3.7.2):
- JustLog (3.8.0):
- SwiftyBeaver (~> 1.9.3)
- SwiftyBeaver (1.9.5)

Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
JustLog: db54ca372b659e3da001d24f6e3c701ed552d87e
JustLog: c4636ea332cf2c677bde5d58d67347880c6cc3e2
SwiftyBeaver: 84069991dd5dca07d7069100985badaca7f0ce82

PODFILE CHECKSUM: c7680d001abb03143de15e13f1b6fbabb258d898
Expand Down
25 changes: 24 additions & 1 deletion Example/Tests/CustomDestinationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}
25 changes: 22 additions & 3 deletions Example/Tests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: []) 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)
}
}
2 changes: 1 addition & 1 deletion JustLog.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'JustLog'
s.version = '3.7.2'
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
Expand Down
10 changes: 6 additions & 4 deletions JustLog/Classes/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit d96562f

Please sign in to comment.