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

Commit

Permalink
Bring internal changes to public origin
Browse files Browse the repository at this point in the history
  • Loading branch information
albertodebortoli committed Jan 2, 2024
1 parent 5a29497 commit 009c95f
Show file tree
Hide file tree
Showing 50 changed files with 1,066 additions and 812 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
2.5.3
3.1.3

1 change: 1 addition & 0 deletions Example/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.3
70 changes: 31 additions & 39 deletions Example/JustLog/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
forceSendLogs(application)
}

private var logger: Logger!

func redactValues(message: String, loggerExeptionList: [ExceptionListResponse], matches: [NSTextCheckingResult]) -> String {

var redactedLogMessage = message
Expand Down Expand Up @@ -81,51 +83,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
identifier = UIBackgroundTaskIdentifier.invalid
})

Logger.shared.forceSend { completionHandler in
logger.forceSend { completionHandler in
application.endBackgroundTask(identifier)
identifier = UIBackgroundTaskIdentifier.invalid
}
}

private func setupLogger() {
let logger = Logger.shared
let decoder = JSONDecoder()

// custom keys
logger.logTypeKey = "logtype"
logger.appVersionKey = "app_version"
logger.iosVersionKey = "ios_version"
logger.deviceTypeKey = "ios_device"

// file destination
logger.logFilename = "justeat-demo.log"

// logstash destination
logger.logstashHost = "listener.logz.io"
logger.logstashPort = 5052
logger.logstashTimeout = 5
logger.logLogstashSocketActivity = true


let regexRuleList = "[{\"pattern\": \"(name) = \\\\\\\\*\\\"(.*?[^\\\\\\\\]+)\", \"minimumLogLevel\": \"warning\"}, {\"pattern\": \"(token) = \\\\\\\\*\\\"(.*?[^\\\\\\\\]+)\", \"minimumLogLevel\": \"warning\"}]".data(using: .utf8)
let sanitizationExeceptionList = "[{\"value\": \"Dan Jones\"}, {\"value\": \"Jack Jones\"}]".data(using: .utf8)


let configuration = Configuration(
logFilename: "justeat-demo.log",
logstashHost: "listener.logz.io",
logstashPort: 5052,
logstashTimeout: 5,
logLogstashSocketActivity: true
)

logger = Logger(
configuration: configuration,
logMessageFormatter: JSONStringLogMessageFormatter(keys: FormatterKeys())
)

let regexRuleList = "[{\"pattern\": \"(name) = \\\\\\\\*\\\"(.*?[^\\\\\\\\]+)\", \"minimumLogLevel\": \"warning\"}, {\"pattern\": \"(token) = \\\\\\\\*\\\"(.*?[^\\\\\\\\]+)\", \"minimumLogLevel\": \"warning\"}]".data(using: .utf8)
let sanitizationExeceptionList = "[{\"value\": \"Dan Jones\"}, {\"value\": \"Jack Jones\"}]".data(using: .utf8)

logger.sanitize = { message, type in
var sanitizedMessage = message

guard let ruleList = try? decoder.decode([RegexListReponse].self, from: regexRuleList!) else { return "sanitizedMessage" }
guard let exceptionList = try? decoder.decode([ExceptionListResponse].self, from: sanitizationExeceptionList!) else { return "sanitizedMessage" }

for value in ruleList {
if (value.minimumLogLevel >= type.rawValue) {
if let regex = try? NSRegularExpression(pattern: value.pattern , options: NSRegularExpression.Options.caseInsensitive) {

let range = NSRange(sanitizedMessage.startIndex..<sanitizedMessage.endIndex, in: sanitizedMessage)
let matches = regex.matches(in: sanitizedMessage, options: [], range: range)

sanitizedMessage = self.redactValues(message: sanitizedMessage, loggerExeptionList: exceptionList, matches: matches)
}

for value in ruleList {
if (value.minimumLogLevel >= type.rawValue) {
if let regex = try? NSRegularExpression(pattern: value.pattern , options: NSRegularExpression.Options.caseInsensitive) {

let range = NSRange(sanitizedMessage.startIndex..<sanitizedMessage.endIndex, in: sanitizedMessage)
let matches = regex.matches(in: sanitizedMessage, options: [], range: range)

sanitizedMessage = self.redactValues(message: sanitizedMessage, loggerExeptionList: exceptionList, matches: matches)
}
}
}
return sanitizedMessage
}
Expand All @@ -135,12 +133,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// untrusted (self-signed) logstash server support
//logger.allowUntrustedServer = <Bool>

// default info
logger.defaultUserInfo = ["application": "JustLog iOS Demo",
"environment": "development",
"session": sessionID]
logger.setup()
}


Expand Down
31 changes: 21 additions & 10 deletions Example/JustLog/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,41 @@ import JustLog
class ViewController: UIViewController {

private var logNumber = 0


private var logger: Logger = Logger(
configuration: Configuration(),
logMessageFormatter: JSONStringLogMessageFormatter(keys: FormatterKeys())
)

@IBAction func verbose() {
Logger.shared.verbose("[\(logNumber)] not so important", userInfo: ["userInfo key": "userInfo value"])
let log = Log(type: .verbose, message: "[\(logNumber)] not so important", customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

@IBAction func debug() {
Logger.shared.debug("[\(logNumber)] something to debug", userInfo: ["userInfo key": "userInfo value"])
let log = Log(type: .debug, message: "[\(logNumber)] not so debug", customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

@IBAction func info() {
Logger.shared.info("[\(logNumber)] a nice information", userInfo: ["userInfo key": "userInfo value"])
let log = Log(type: .info, message: "[\(logNumber)] a nice information", customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

@IBAction func warning() {
Logger.shared.warning("[\(logNumber)] oh no, that won’t be good", userInfo: ["userInfo key": "userInfo value"])
let log = Log(type: .warning, message: "[\(logNumber)] oh no, that won’t be good", customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

@IBAction func warningSanitized() {
let messageToSanitize = "conversation ={\\n id = \\\"123455\\\";\\n};\\n from = {\\n id = 123456;\\n name = \\\"John Smith\\\";\\n; \\n token = \\\"123456\\\";\\n"
let sanitizedMessage = Logger.shared.sanitize(messageToSanitize, Logger.LogType.warning)
Logger.shared.warning(sanitizedMessage, userInfo: ["userInfo key": "userInfo value"])
let sanitizedMessage = logger.sanitize(messageToSanitize, LogType.warning)
let log = Log(type: .warning, message: sanitizedMessage, customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

Expand All @@ -57,16 +67,17 @@ class ViewController: UIViewController {

let unreadableError = NSError(domain: "com.just-eat.test", code: 1234, userInfo: unreadableUserInfos)

Logger.shared.error("[\(logNumber)] ouch, an error did occur!", error: unreadableError, userInfo: ["userInfo key": "userInfo value"])
let log = Log(type: .error, message: "[\(logNumber)] ouch, an error did occur!", error: unreadableError, customData: ["userInfo key": "userInfo value"])
logger.send(log)
logNumber += 1
}

@IBAction func forceSend() {
Logger.shared.forceSend()
logger.forceSend()
}

@IBAction func cancel(_ sender: Any) {
Logger.shared.cancelSending()
logger.cancelSending()
}
}

6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- JustLog (4.0.0):
- JustLog (4.0.3):
- SwiftyBeaver (= 1.9.6)
- SwiftyBeaver (1.9.6)

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

SPEC CHECKSUMS:
JustLog: e447d77125f8224bc006f280aaa858b00240b2d5
JustLog: c2bb751e8b6f5c14d17dcc301834fd6534c0482a
SwiftyBeaver: c8241b4745aa269a78ca5a022532c6915e948536

PODFILE CHECKSUM: 6e0bf13c9ce9221fdbf6f8bd0ca7a205dcdaeb98

COCOAPODS: 1.10.2
COCOAPODS: 1.14.3
26 changes: 9 additions & 17 deletions Example/Tests/CustomDestinationTests.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
//
// CustomDestinationTests.swift
// JustLog_Tests
//
// Created by Antonio Strijdom on 02/02/2021.
// Copyright © 2021 Just Eat. All rights reserved.
//

import XCTest
@testable import JustLog
Expand All @@ -25,7 +19,7 @@ class MockCustomDestinationSender: CustomDestinationSender {
}

class CustomDestinationTests: XCTestCase {

func testBasicLogging() throws {
let expect = expectation(description: "Send log expectation")
expect.expectedFulfillmentCount = 5
Expand All @@ -43,20 +37,18 @@ class CustomDestinationTests: XCTestCase {
}

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)

let sut = Logger(configuration: Configuration(), logMessageFormatter: JSONStringLogMessageFormatter(keys: FormatterKeys()), customLogSender: 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)
sut.send(Log(type: .verbose, message: "Verbose Message"))
sut.send(Log(type: .debug, message: "Debug Message"))
sut.send(Log(type: .info, message: "Info Message"))
sut.send(Log(type: .warning, message: "Warning Message"))
sut.send(Log(type: .error, message: "Error Message"))

mockSender.logs.forEach { XCTAssertTrue($0.contains("device_timestamp")) }
self.waitForExpectations(timeout: 10.0, handler: nil)
Expand Down
3 changes: 2 additions & 1 deletion Example/Tests/Data_RepresentationTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Data_RepresentationTests.swift

import Foundation
import XCTest
@testable import JustLog
Expand All @@ -14,5 +16,4 @@ class Data_RepresentationTests: XCTestCase {

XCTAssertEqual(representation, target)
}

}
18 changes: 10 additions & 8 deletions Example/Tests/Dictionary_FlatteningTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Dictionary_FlatteningTests.swift

import Foundation
import XCTest
@testable import JustLog
Expand All @@ -12,19 +14,18 @@ class Dictionary_Flattening: XCTestCase {
let merged = d1.merged(with: d2)
let target = ["k1": "v1", "k2": "v2", "k3": "v3", "k4": "v4"]

XCTAssertEqual(NSDictionary(dictionary:merged), NSDictionary(dictionary: target))
XCTAssertEqual(NSDictionary(dictionary: merged), NSDictionary(dictionary: target))
}

func test_merge_withConflictingDictionies() {

let d1 = ["k1": "v1", "k2": "v2"]
let d2 = ["k1": "v1b", "k3": "v3"]


let merged = d1.merged(with: d2)
let target = ["k1": "v1b", "k2": "v2", "k3": "v3"]

XCTAssertEqual(NSDictionary(dictionary:merged), NSDictionary(dictionary:target))
XCTAssertEqual(NSDictionary(dictionary: merged), NSDictionary(dictionary: target))
}

func test_flattened() {
Expand All @@ -33,7 +34,7 @@ class Dictionary_Flattening: XCTestCase {
let error = NSError(domain: domain, code: 200, userInfo: ["k8": "v8"])
let nestedError = NSError(domain: domain, code: 200, userInfo: [NSUnderlyingErrorKey: NSError(domain: domain, code: 200, userInfo: ["k10": 10])])
let input = ["k1": "v1", "k2": ["k3": "v3"], "k4": 4, "k5": 5.1, "k6": true,
"k7": error, "k9": nestedError, "k11": [1, 2, 3]] as [String : Any]
"k7": error, "k9": nestedError, "k11": [1, 2, 3]] as [String: Any]

let flattened = input.flattened()

Expand All @@ -50,7 +51,7 @@ class Dictionary_Flattening: XCTestCase {

func test_flattened_withConflictingDictionies() {

let input = ["k1": "v1", "k2": ["k1": "v3", "k4": "v4"]] as [String : Any]
let input = ["k1": "v1", "k2": ["k1": "v3", "k4": "v4"]] as [String: Any]

let flattened = input.flattened()
// can be either ["k1": "v1", "k4": "v4"] or ["k1": "v3", "k4": "v4"]
Expand All @@ -62,9 +63,10 @@ class Dictionary_Flattening: XCTestCase {

func test_flattened_recursive() {

let input = ["k1": "v1",
"k2": ["k3": "v3",
"k4": ["k5": "v5"]]] as [String : Any]
let input: [String: Any] = [
"k1": "v1",
"k2": ["k3": "v3", "k4": ["k5": "v5"]] as [String: Any]
]

let flattened = input.flattened()
// target = ["k1": "v1", "k3": "v3", "k5": "v5"] as [String : Any]
Expand Down
19 changes: 10 additions & 9 deletions Example/Tests/Dictionary_JSONTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Dictionary_JSONTests.swift

import Foundation
import XCTest
@testable import JustLog
Expand All @@ -11,25 +13,24 @@ class Dictionary_JSON: XCTestCase {
"k3": 42,
"k4": true,
"k5": [1, 2],
"k6": ["k": "v"]] as [String : Any]
"k6": ["k": "v"]] as [String: Any]

let jsonRepresentation = input.toJSON()!

XCTAssertTrue(jsonRepresentation.range(of: "\"k1\":\"v1\"") != nil)
XCTAssertTrue(jsonRepresentation.range(of: "\"k2\":2.5") != nil)
XCTAssertTrue(jsonRepresentation.range(of: "\"k3\":42") != nil)
XCTAssertTrue(jsonRepresentation.range(of: "\"k4\":true") != nil)
XCTAssertTrue(jsonRepresentation.range(of: "\"k5\":[1,2]") != nil)
XCTAssertTrue(jsonRepresentation.range(of: "\"k6\":{\"k\":\"v\"}") != nil)
XCTAssertTrue(jsonRepresentation.contains("\"k1\":\"v1\""))
XCTAssertTrue(jsonRepresentation.contains("\"k2\":2.5"))
XCTAssertTrue(jsonRepresentation.contains("\"k3\":42"))
XCTAssertTrue(jsonRepresentation.contains("\"k4\":true"))
XCTAssertTrue(jsonRepresentation.contains("\"k5\":[1,2]"))
XCTAssertTrue(jsonRepresentation.contains("\"k6\":{\"k\":\"v\"}"))
}

func test_JSON_invalid() {

let input = ["k1": "v1".data(using: String.Encoding.utf8)!] as [String : Any]
let input = ["k1": "v1".data(using: String.Encoding.utf8)!] as [String: Any]

let jsonRepresentation = input.toJSON()

XCTAssertNil(jsonRepresentation)
}

}
Loading

0 comments on commit 009c95f

Please sign in to comment.