kean / Pulse

Network logger for Apple platforms
https://pulselogger.com
MIT License
6.32k stars 302 forks source link

Logging Network Request to XCode Console #93

Closed savage7 closed 2 years ago

savage7 commented 2 years ago

I've found this question: https://github.com/kean/Pulse/issues/71

I want all network requests also log to the Xcode console.

I've configured Swift Log with these backends:

LoggingSystem.bootstrap {
                MultiplexLogHandler([
                    Pulse.PersistentLogHandler(label: $0),
                    StreamLogHandler.standardOutput(label: $0)
                ])
}

Logging messages via Swift Log now logs to Pulse.

Is it currently possible to also get the network logs in the Xcode console too? I looked at the NetworkLogger code, which seams to log only to the store.

kean commented 2 years ago

Hi,

NetworkLogger is part of the PulseCore framework and doesn't have SwiftLog dependency, so it can't log to the SwiftLog system. I'm not sure how to address this cleanly.

If you are using Pulse 2.0, you can set set willHandleEvent closure on NetworkLogger.Configuration or listen to LoggerStore/events to listen to all log events processed by the loggers, including all network events. You can leverage that to log the info you need about network requests.

I'm not sure it's a good idea to build this into the framework, as the console output is a limited resource. I don't want to pollute it with too much information, even if was an optional feature. I think the event observer in Pulse 2.0 offers a nice compromise. It's easy to monitor all events and you can format them any way you'd like and skip the info you are not interested in.

kean commented 2 years ago

Example:

func register(store: LoggerStore) {
    cancellable = store.events.receive(on: queue).sink { [weak self] in
        self?.process(event: $0)
    }
}

private func process(event: LoggerStore.Event) {
    switch event {
    case .messageStored: break
    case .networkTaskCreated: break
    case .networkTaskProgressUpdated: break
    case .networkTaskCompleted(let event):
        os_log("<any information you need from event in any format you like>")
    }
}
savage7 commented 2 years ago

@kean Thanks a lot for your help and for Pulse and Get. Both a real super cool :heart:

Just swichted to 2.0 and this works perfectly. With 1.0 I ended up implementing a URLSessionDataDelegate and adding the logs myself.