p2 / OAuth2

OAuth2 framework for macOS and iOS, written in Swift.
Other
1.14k stars 277 forks source link

Allow to specify an OAuth2Logger already during initialization #420

Open berg-do opened 5 months ago

berg-do commented 5 months ago

I am assigning a custom logger class to my oAuth2 instance (using the OAuth2CodeGrant).

Unfortunately in verbose mode the superclass (ultimately the OAuth2Requestable) will automatically create a OAuth2DebugLogger internally and log to it before there is the chance to apply a custom logger to the oAuth2 instance.

This results in a log like this:

[Debug] OAuth2: Initialization finished
[Debug] OAuth2: Looking for items in keychain
[Debug] OAuth2: Found access token, valid until 2024-04-22 09:50:02 +0000
[Debug] OAuth2: Found refresh token
[Debug] OAuth2: Found id token
2024-04-22 11:48:58.523 [INFO] Something, something, my own logging

Extending the OAuth2DebugLogger to implement my own logger logic does not seem to work. The extended apparently does not get called for some reason. I'm not entirely sure why, but it may be due to the OAuth2 dependency not calling stuff from classes that got extended outside the dependency itself.

The following sadly does not do the trick.

extension OAuth2DebugLogger {
    public func log(_ atLevel: OAuth2LogLevel, module: String?, filename: String?, line: Int?, function: String?, msg: @autoclosure () -> String) {
        MyLogger.log(level, atLevel, module, msg())
    }
}

My suggestion would be to allow specifying a logger instance during the OAuth2CodeGrant(settings: []) initializer that will already be used for verbose logging.