Skip to content

Commit

Permalink
Merge pull request #4 from allaboutapps/feature/detailedClosureLogs
Browse files Browse the repository at this point in the history
Add callback with detailed log informations such as filename, line, ...
  • Loading branch information
wieweb committed Dec 9, 2022
2 parents 8ea1540 + c88145a commit 4183352
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Logbook/Source/Sink/ClosureLogSink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ public class ClosureLogSink: LogSink {
public var format: String = LogPlaceholder.defaultLogFormat
public var dateFormatter: DateFormatter

private let callback: (String) -> Void
private let callback: ((String) -> Void)?
private let detailedCallback: ((LogMessage) -> Void)?

public init(identifier: String = UUID().uuidString, level: LevelMode, categories: LogCategoryFilter = .all, callback: @escaping (String) -> Void) {
public init(identifier: String = UUID().uuidString,
level: LevelMode,
categories: LogCategoryFilter = .all,
callback: ((String) -> Void)? = nil,
detailedCallback: ((LogMessage) -> Void)? = nil) {
self.identifier = identifier
self.level = level
self.categories = categories
self.dateFormatter = DateFormatter()
self.dateFormatter.dateStyle = .short
self.dateFormatter.timeStyle = .medium
self.callback = callback
self.detailedCallback = detailedCallback
}

public func send(_ message: LogMessage) {
Expand All @@ -44,7 +50,8 @@ public class ClosureLogSink: LogSink {
formattedMessage = formattedMessage.replacingOccurrences(of: LogPlaceholder.line, with: "\(message.header.line)")
formattedMessage = formattedMessage.replacingOccurrences(of: LogPlaceholder.messages, with: messages)

callback(formattedMessage)
callback?(formattedMessage)
detailedCallback?(message)
}

}
42 changes: 42 additions & 0 deletions LogbookTests/ClosureLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,46 @@ class ClosureLogTests: XCTestCase {
XCTAssertEqual(logMessage, "TestError.missingData")
}
}

func testDetailedClosureSink() {
let testMessage = "Test Message"

var category: String = ""
var level: LogLevel = .error
var message: String = ""

var file: String = ""
var function: String = ""
var line: UInt = 0

let expectation = expectation(description: "expectation")

let sink = ClosureLogSink(level: .min(.debug), detailedCallback: { log in
category = log.category.identifier
level = log.level
message = log.messages.first ?? ""

file = log.header.file
function = log.header.function
line = log.header.line

expectation.fulfill()
})

let logFormat = "\(LogPlaceholder.messages)"
sink.format = logFormat
logbook.add(sink: sink)

logbook.debug(testMessage, category: .networking)

waitForExpectations(timeout: 3) { _ in
XCTAssertEqual(category, LogCategory.networking.identifier)
XCTAssertEqual(level, LogLevel.debug)
XCTAssertEqual(message, testMessage)

XCTAssertTrue(file.contains("ClosureLogTests.swift"))
XCTAssertEqual(function, "testDetailedClosureSink()")
XCTAssertNotEqual(line, 0)
}
}
}

0 comments on commit 4183352

Please sign in to comment.