Skip to content

Commit 1f39437

Browse files
committed
Use swift-log as logging system
1 parent b94b0b4 commit 1f39437

23 files changed

+97
-238
lines changed

OAuth2.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@
118118
EEACE1DD1A7E8DF7009BF3A7 /* extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEDB864B19421DAD00C4EEA1 /* extensions.swift */; };
119119
EEACE1DF1A7E8FC1009BF3A7 /* OAuth2CodeGrantFacebook.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEACE1DE1A7E8FC1009BF3A7 /* OAuth2CodeGrantFacebook.swift */; };
120120
EEACE1E01A7E8FC5009BF3A7 /* OAuth2CodeGrantFacebook.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEACE1DE1A7E8FC1009BF3A7 /* OAuth2CodeGrantFacebook.swift */; };
121-
EEAEF10B1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEAEF10A1CDBCF28001A1C6F /* OAuth2Logger.swift */; };
122-
EEAEF10C1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEAEF10A1CDBCF28001A1C6F /* OAuth2Logger.swift */; };
123-
EEAEF10D1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEAEF10A1CDBCF28001A1C6F /* OAuth2Logger.swift */; };
124121
EEB9A97C1D86C34E0022EF66 /* OAuth2Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEB9A97B1D86C34E0022EF66 /* OAuth2Response.swift */; };
125122
EEB9A97D1D86C34E0022EF66 /* OAuth2Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEB9A97B1D86C34E0022EF66 /* OAuth2Response.swift */; };
126123
EEB9A97E1D86C34E0022EF66 /* OAuth2Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEB9A97B1D86C34E0022EF66 /* OAuth2Response.swift */; };
@@ -239,7 +236,6 @@
239236
EE9EBF1A1D775F74003263FC /* OAuth2Securable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2Securable.swift; sourceTree = "<group>"; };
240237
EEAC17A01CAA69110025F84B /* CONTRIBUTORS.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTORS.md; sourceTree = "<group>"; };
241238
EEACE1DE1A7E8FC1009BF3A7 /* OAuth2CodeGrantFacebook.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2CodeGrantFacebook.swift; sourceTree = "<group>"; };
242-
EEAEF10A1CDBCF28001A1C6F /* OAuth2Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2Logger.swift; sourceTree = "<group>"; };
243239
EEB9A97B1D86C34E0022EF66 /* OAuth2Response.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2Response.swift; sourceTree = "<group>"; };
244240
EEB9A97F1D86CD4A0022EF66 /* OAuth2DataLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2DataLoaderTests.swift; sourceTree = "<group>"; };
245241
EEB9A9821D86D36A0022EF66 /* OAuth2RequestPerformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2RequestPerformer.swift; sourceTree = "<group>"; };
@@ -355,7 +351,6 @@
355351
EEB9A9821D86D36A0022EF66 /* OAuth2RequestPerformer.swift */,
356352
EEB9A97B1D86C34E0022EF66 /* OAuth2Response.swift */,
357353
EE79F6591BFAA36900746243 /* OAuth2Error.swift */,
358-
EEAEF10A1CDBCF28001A1C6F /* OAuth2Logger.swift */,
359354
65EC05DF1C9050CB00DE9186 /* OAuth2KeychainAccount.swift */,
360355
EE29EABB195A0DB2008882C8 /* OAuth2DebugURLSessionDelegate.swift */,
361356
EE2983741D40BE7600933CDD /* OAuth2AuthorizerUI.swift */,
@@ -739,7 +734,6 @@
739734
EE9EBF151D775A21003263FC /* OAuth2DataLoader.swift in Sources */,
740735
EE1070361E5C7A4200250586 /* OAuth2CustomAuthorizerUI.swift in Sources */,
741736
87FABDDE2DC9F2DF00E0C67B /* OAuth2Actor.swift in Sources */,
742-
EEAEF10D1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */,
743737
EE9EBF1D1D775F74003263FC /* OAuth2Securable.swift in Sources */,
744738
659854521C5B3C9C00237D39 /* OAuth2AuthConfig.swift in Sources */,
745739
EEC49F331C9BF22400989A18 /* OAuth2AuthRequest.swift in Sources */,
@@ -754,7 +748,6 @@
754748
buildActionMask = 2147483647;
755749
files = (
756750
EEACE1DD1A7E8DF7009BF3A7 /* extensions.swift in Sources */,
757-
EEAEF10C1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */,
758751
EE20118D1E44D0BD00913FA7 /* OAuth2DataLoaderSessionTaskDelegate.swift in Sources */,
759752
65EC05E11C9050CB00DE9186 /* OAuth2KeychainAccount.swift in Sources */,
760753
87FABDDF2DC9F2DF00E0C67B /* OAuth2Actor.swift in Sources */,
@@ -798,7 +791,6 @@
798791
buildActionMask = 2147483647;
799792
files = (
800793
EEACE1DC1A7E8DF6009BF3A7 /* extensions.swift in Sources */,
801-
EEAEF10B1CDBCF28001A1C6F /* OAuth2Logger.swift in Sources */,
802794
65EC05E01C9050CB00DE9186 /* OAuth2KeychainAccount.swift in Sources */,
803795
0C2F5E5B1DE2DB8500F621E0 /* OAuth2CodeGrantAzure.swift in Sources */,
804796
DD0CCBAD1C4DC83A0044C4E3 /* OAuth2WebViewController+macOS.swift in Sources */,

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ oauth2.authConfig.authorizeContext = <# your UIViewController / NSWindow #>
6868

6969
Need to specify a separate refresh token URI? You can set the `refresh_uri` in the Settings Dictionary. If specified the library will refresh access tokens using the `refresh_uri` you specified, otherwise it will use the `token_uri`.
7070

71-
Need to debug? Use a `.debug` or even a `.trace` logger:
72-
73-
```swift
74-
oauth2.logger = OAuth2DebugLogger(.trace)
75-
```
71+
Need to debug? Set `LogHandler.logLevel` parameter to `.debug` or even `.trace` for specified handler.
7672

7773
For more see [advanced settings](#advanced-settings) below.
7874

Sources/Base/OAuth2AuthRequest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ open class OAuth2AuthRequest {
189189

190190
// add to request body
191191
if oauth2.clientConfig.secretInBody {
192-
oauth2.logger?.debug("OAuth2", msg: "Adding “client_id” and “client_secret” to request body")
192+
oauth2.logger?.debug("Adding “client_id” and “client_secret” to request body")
193193
finalParams["client_id"] = clientId
194194
finalParams["client_secret"] = secret
195195
}
196196

197197
// add Authorization header (if not in body)
198198
else {
199-
oauth2.logger?.debug("OAuth2", msg: "Adding “Authorization” header as “Basic client-key:client-secret”")
199+
oauth2.logger?.debug("Adding “Authorization” header as “Basic client-key:client-secret”")
200200
let pw = "\(clientId.wwwFormURLEncodedString):\(secret.wwwFormURLEncodedString)"
201201
if let utf8 = pw.data(using: oauth2.clientConfig.authStringEncoding) {
202202
req.setValue("Basic \(utf8.base64EncodedString())", forHTTPHeaderField: "Authorization")
@@ -212,13 +212,13 @@ open class OAuth2AuthRequest {
212212
// add custom headers, first from our OAuth2 instance, then our custom ones
213213
if let headers = oauth2.authHeaders {
214214
for (key, val) in headers {
215-
oauth2.logger?.trace("OAuth2", msg: "Overriding “\(key)” header")
215+
oauth2.logger?.trace("Overriding “\(key)” header")
216216
req.setValue(val, forHTTPHeaderField: key)
217217
}
218218
}
219219
if let headers = headers {
220220
for (key, val) in headers {
221-
oauth2.logger?.trace("OAuth2", msg: "Adding custom “\(key)” header")
221+
oauth2.logger?.trace("Adding custom “\(key)” header")
222222
req.setValue(val, forHTTPHeaderField: key)
223223
}
224224
}

Sources/Base/OAuth2Base.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ open class OAuth2Base: OAuth2Securable {
201201

202202
override open func updateFromKeychainItems(_ items: [String: any Sendable]) {
203203
for message in clientConfig.updateFromStorableItems(items) {
204-
logger?.debug("OAuth2", msg: message)
204+
logger?.debug("\(message)")
205205
}
206206
clientConfig.secretInBody = (clientConfig.endpointAuthMethod == OAuth2EndpointAuthMethod.clientSecretPost)
207207
}
@@ -298,7 +298,7 @@ open class OAuth2Base: OAuth2Securable {
298298
public final func didFail(with error: OAuth2Error?) {
299299
var finalError = error
300300
if let error = finalError {
301-
logger?.debug("OAuth2", msg: "\(error)")
301+
logger?.debug("\(error)")
302302
}
303303
else {
304304
finalError = OAuth2Error.requestCancelled
@@ -319,7 +319,7 @@ open class OAuth2Base: OAuth2Securable {
319319
*/
320320
open func abortAuthorization() {
321321
if !abortTask() {
322-
logger?.debug("OAuth2", msg: "Aborting authorization")
322+
logger?.debug("Aborting authorization")
323323
didFail(with: nil)
324324
}
325325
}
@@ -472,7 +472,7 @@ open class OAuth2Base: OAuth2Securable {
472472
guard let state = params["state"] as? String, !state.isEmpty else {
473473
throw OAuth2Error.missingState
474474
}
475-
logger?.trace("OAuth2", msg: "Checking state, got “\(state)”, expecting “\(context.state)")
475+
logger?.trace("Checking state, got “\(state)”, expecting “\(context.state)")
476476
if !context.matchesState(state) {
477477
throw OAuth2Error.invalidState
478478
}

Sources/Base/OAuth2Logger.swift

Lines changed: 0 additions & 132 deletions
This file was deleted.

Sources/Base/OAuth2Requestable.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//
2020

2121
import Foundation
22+
import Logging
2223

2324

2425
/// Typealias to ease working with JSON dictionaries.
@@ -40,32 +41,34 @@ open class OAuth2Requestable {
4041
/// Set to `true` to log all the things. `false` by default. Use `"verbose": bool` in settings or assign `logger` yourself.
4142
open var verbose = false {
4243
didSet {
43-
logger = verbose ? OAuth2DebugLogger() : nil
44+
logger = verbose ? Logger(label: "OAuth2") : nil
45+
logger?.logLevel = .debug // make debug logger
4446
}
4547
}
4648

4749
/// The logger being used. Auto-assigned to a debug logger if you set `verbose` to true or false.
48-
open var logger: OAuth2Logger?
50+
open var logger: Logger?
4951

5052

5153
/**
5254
Base initializer.
5355
*/
5456
public init(verbose: Bool) {
5557
self.verbose = verbose
56-
logger = verbose ? OAuth2DebugLogger() : nil
57-
logger?.debug("OAuth2", msg: "Initialization finished")
58+
logger = verbose ? Logger(label: "OAuth2") : nil
59+
logger?.logLevel = .debug // make debug logger
60+
logger?.debug("Initialization finished")
5861
}
5962

6063
/**
6164
Designated initializer.
6265

63-
- parameter logger: An optional `OAuth2Logger` instance to use
66+
- parameter logger: An optional `Logger` (swift-log) instance to use
6467
*/
65-
public init(logger: OAuth2Logger?) {
68+
public init(logger: Logger?) {
6669
self.logger = logger
6770
self.verbose = (nil != logger)
68-
logger?.debug("OAuth2", msg: "Initialization finished")
71+
logger?.debug("Initialization finished")
6972
}
7073

7174

@@ -116,14 +119,14 @@ open class OAuth2Requestable {
116119
- returns : OAuth2 response
117120
*/
118121
open func perform(request: URLRequest) async -> OAuth2Response {
119-
self.logger?.trace("OAuth2", msg: "REQUEST\n\(request.debugDescription)\n---")
122+
self.logger?.trace("REQUEST\n\(request.debugDescription)\n---")
120123
let performer = requestPerformer ?? OAuth2DataTaskRequestPerformer(session: session)
121124
requestPerformer = performer
122125

123126
do {
124127
// TODO: add support for aborting the request, see https://www.hackingwithswift.com/quick-start/concurrency/how-to-cancel-a-task
125128
let (sessData, sessResponse) = try await performer.perform(request: request)
126-
self.logger?.trace("OAuth2", msg: "RESPONSE\n\(sessResponse.debugDescription)\n\n\(String(data: sessData ?? Data(), encoding: String.Encoding.utf8) ?? "no data")\n---")
129+
self.logger?.trace("RESPONSE\n\(sessResponse.debugDescription)\n\n\(String(data: sessData ?? Data(), encoding: String.Encoding.utf8) ?? "no data")\n---")
127130

128131
guard let response = sessResponse as? HTTPURLResponse else {
129132
throw CommonError.castError(
@@ -135,7 +138,7 @@ open class OAuth2Requestable {
135138
return OAuth2Response(data: sessData, request: request, response: response, error: nil)
136139

137140
} catch {
138-
self.logger?.trace("OAuth2", msg: "RESPONSE\nno response\n\nno data\n---")
141+
self.logger?.trace("RESPONSE\nno response\n\nno data\n---")
139142

140143
let http = HTTPURLResponse(url: request.url!, statusCode: 499, httpVersion: nil, headerFields: nil)!
141144
return OAuth2Response(data: nil, request: request, response: http, error: error)
@@ -154,7 +157,7 @@ open class OAuth2Requestable {
154157
guard let task = abortableTask else {
155158
return false
156159
}
157-
logger?.debug("OAuth2", msg: "Aborting request")
160+
logger?.debug("Aborting request")
158161
task.cancel()
159162
return true
160163
}
@@ -175,13 +178,13 @@ open class OAuth2Requestable {
175178
return json
176179
}
177180
if let str = String(data: data, encoding: String.Encoding.utf8) {
178-
logger?.warn("OAuth2", msg: "JSON did not resolve to a dictionary, was: \(str)")
181+
logger?.warning("JSON did not resolve to a dictionary, was: \(str)")
179182
}
180183
throw OAuth2Error.jsonParserError
181184
}
182185
catch let error where NSCocoaErrorDomain == error._domain && 3840 == error._code { // JSON parser error
183186
if let str = String(data: data, encoding: String.Encoding.utf8) {
184-
logger?.warn("OAuth2", msg: "Unparsable JSON was: \(str)")
187+
logger?.warning("Unparsable JSON was: \(str)")
185188
}
186189
throw OAuth2Error.jsonParserError
187190
}

0 commit comments

Comments
 (0)