Skip to content

Commit

Permalink
Merge pull request #16 from GoodRequest/feat/messagePublisher
Browse files Browse the repository at this point in the history
feat: message publisher
  • Loading branch information
andrej-jasso authored Mar 5, 2024
2 parents c304973 + 4027f16 commit 1cce094
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Sources/GoodNetworking/Session/Logger/LoggingEventMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

import Foundation
import Alamofire
import Combine

public class LoggingEventMonitor: EventMonitor {

public static var verbose: Bool = true
public static var prettyPrinted: Bool = true
public static var maxVerboseLogSizeBytes: Int = 100_000

private var messages: PassthroughSubject<String, Never>?
public lazy var messagesPublisher = messages?.eraseToAnyPublisher()

public let queue = DispatchQueue(label: C.queueLabel, qos: .background)

private enum C {
Expand All @@ -22,9 +26,9 @@ public class LoggingEventMonitor: EventMonitor {

}

private var logger: any SessionLogger
private var logger: (any SessionLogger)?

public init(logger: any SessionLogger) {
public init(logger: (any SessionLogger)?) {
self.logger = logger
}

Expand All @@ -44,19 +48,23 @@ public class LoggingEventMonitor: EventMonitor {
"❓❓❓ Response MIME type not whitelisted (\(response.response?.mimeType ?? "")). You can try adding it to whitelist using logMimeType(_ mimeType:)."
}

let logMessaage = [
let logMessage = [
requestInfoMessage,
metricsMessage,
requestBodyMessage,
errorMessage,
responseBodyMessage
].compactMap { $0 }.joined(separator: "\n")

if let messages {
messages.send(logMessage)
}

switch response.result {
case .success:
logger.log(level: .debug, message: logMessaage)
logger?.log(level: .debug, message: logMessage)
case .failure:
logger.log(level: .fault, message: logMessaage)
logger?.log(level: .fault, message: logMessage)
}
}

Expand Down

0 comments on commit 1cce094

Please sign in to comment.