Skip to content

Commit 67b126c

Browse files
committed
fix(logging): improve python core's debug logging
Signed-off-by: Phil Adams <[email protected]>
1 parent d603ed0 commit 67b126c

39 files changed

+331
-137
lines changed

.secrets.baseline

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "package-lock.json|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2024-02-26T20:31:05Z",
6+
"generated_at": "2024-09-16T18:49:02Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -222,7 +222,7 @@
222222
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
223223
"is_secret": false,
224224
"is_verified": false,
225-
"line_number": 112,
225+
"line_number": 133,
226226
"type": "Hex High Entropy String",
227227
"verified_result": null
228228
}
@@ -242,31 +242,31 @@
242242
"hashed_secret": "c8f0df25bade89c1873f5f01b85bcfb921443ac6",
243243
"is_secret": false,
244244
"is_verified": false,
245-
"line_number": 30,
245+
"line_number": 34,
246246
"type": "JSON Web Token",
247247
"verified_result": null
248248
},
249249
{
250250
"hashed_secret": "f06e1073ca9afdd800a2cf27f944d06530b5b755",
251251
"is_secret": false,
252252
"is_verified": false,
253-
"line_number": 31,
253+
"line_number": 35,
254254
"type": "JSON Web Token",
255255
"verified_result": null
256256
},
257257
{
258258
"hashed_secret": "360c23c1ac7d9d6dad1d0710606b0df9de6e1a18",
259259
"is_secret": false,
260260
"is_verified": false,
261-
"line_number": 35,
261+
"line_number": 39,
262262
"type": "Secret Keyword",
263263
"verified_result": null
264264
},
265265
{
266266
"hashed_secret": "62cdb7020ff920e5aa642c3d4066950dd1f01f4d",
267267
"is_secret": false,
268268
"is_verified": false,
269-
"line_number": 139,
269+
"line_number": 146,
270270
"type": "Secret Keyword",
271271
"verified_result": null
272272
}
@@ -276,15 +276,15 @@
276276
"hashed_secret": "5eb942810a75ebc850972a89285d570d484c89c4",
277277
"is_secret": false,
278278
"is_verified": false,
279-
"line_number": 91,
279+
"line_number": 95,
280280
"type": "Secret Keyword",
281281
"verified_result": null
282282
},
283283
{
284284
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
285285
"is_secret": false,
286286
"is_verified": false,
287-
"line_number": 132,
287+
"line_number": 136,
288288
"type": "Hex High Entropy String",
289289
"verified_result": null
290290
}
@@ -294,7 +294,7 @@
294294
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
295295
"is_secret": false,
296296
"is_verified": false,
297-
"line_number": 28,
297+
"line_number": 48,
298298
"type": "Hex High Entropy String",
299299
"verified_result": null
300300
}
@@ -304,23 +304,23 @@
304304
"hashed_secret": "4080eeeaf54faf879b9e8d99c49a8503f7e855bb",
305305
"is_secret": false,
306306
"is_verified": false,
307-
"line_number": 71,
307+
"line_number": 75,
308308
"type": "Secret Keyword",
309309
"verified_result": null
310310
},
311311
{
312312
"hashed_secret": "37e94c31b6a756ba2afd2fe9a9765172cd79ac47",
313313
"is_secret": false,
314314
"is_verified": false,
315-
"line_number": 93,
315+
"line_number": 97,
316316
"type": "Secret Keyword",
317317
"verified_result": null
318318
},
319319
{
320320
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
321321
"is_secret": false,
322322
"is_verified": false,
323-
"line_number": 113,
323+
"line_number": 117,
324324
"type": "Hex High Entropy String",
325325
"verified_result": null
326326
}
@@ -330,39 +330,39 @@
330330
"hashed_secret": "c8f0df25bade89c1873f5f01b85bcfb921443ac6",
331331
"is_secret": false,
332332
"is_verified": false,
333-
"line_number": 28,
333+
"line_number": 32,
334334
"type": "JSON Web Token",
335335
"verified_result": null
336336
},
337337
{
338338
"hashed_secret": "f06e1073ca9afdd800a2cf27f944d06530b5b755",
339339
"is_secret": false,
340340
"is_verified": false,
341-
"line_number": 29,
341+
"line_number": 33,
342342
"type": "JSON Web Token",
343343
"verified_result": null
344344
},
345345
{
346346
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
347347
"is_secret": false,
348348
"is_verified": false,
349-
"line_number": 52,
349+
"line_number": 56,
350350
"type": "Hex High Entropy String",
351351
"verified_result": null
352352
},
353353
{
354354
"hashed_secret": "b3f00e146afe19aab0069029b7fb3926ad756d26",
355355
"is_secret": false,
356356
"is_verified": false,
357-
"line_number": 129,
357+
"line_number": 134,
358358
"type": "Hex High Entropy String",
359359
"verified_result": null
360360
},
361361
{
362362
"hashed_secret": "62cdb7020ff920e5aa642c3d4066950dd1f01f4d",
363363
"is_secret": false,
364364
"is_verified": false,
365-
"line_number": 191,
365+
"line_number": 196,
366366
"type": "Secret Keyword",
367367
"verified_result": null
368368
}
@@ -372,7 +372,7 @@
372372
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
373373
"is_secret": false,
374374
"is_verified": false,
375-
"line_number": 35,
375+
"line_number": 39,
376376
"type": "Hex High Entropy String",
377377
"verified_result": null
378378
}
@@ -382,15 +382,15 @@
382382
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
383383
"is_secret": false,
384384
"is_verified": false,
385-
"line_number": 91,
385+
"line_number": 95,
386386
"type": "Hex High Entropy String",
387387
"verified_result": null
388388
},
389389
{
390390
"hashed_secret": "f2e7745f43b0ef0e2c2faf61d6c6a28be2965750",
391391
"is_secret": false,
392392
"is_verified": false,
393-
"line_number": 147,
393+
"line_number": 151,
394394
"type": "Secret Keyword",
395395
"verified_result": null
396396
}
@@ -400,23 +400,23 @@
400400
"hashed_secret": "da2f27d2c57a0e1ed2dc3a34b4ef02faf2f7a4c2",
401401
"is_secret": false,
402402
"is_verified": false,
403-
"line_number": 30,
403+
"line_number": 50,
404404
"type": "Hex High Entropy String",
405405
"verified_result": null
406406
},
407407
{
408408
"hashed_secret": "f2e7745f43b0ef0e2c2faf61d6c6a28be2965750",
409409
"is_secret": false,
410410
"is_verified": false,
411-
"line_number": 43,
411+
"line_number": 63,
412412
"type": "Secret Keyword",
413413
"verified_result": null
414414
},
415415
{
416416
"hashed_secret": "ace1a5bf229c3af3f699369c6f2fa1a628692ab8",
417417
"is_secret": false,
418418
"is_verified": false,
419-
"line_number": 60,
419+
"line_number": 81,
420420
"type": "Secret Keyword",
421421
"verified_result": null
422422
}
@@ -426,31 +426,31 @@
426426
"hashed_secret": "34a0a47a51d5bf739df0214450385e29ee7e9847",
427427
"is_secret": false,
428428
"is_verified": false,
429-
"line_number": 435,
429+
"line_number": 439,
430430
"type": "Secret Keyword",
431431
"verified_result": null
432432
},
433433
{
434434
"hashed_secret": "f2e7745f43b0ef0e2c2faf61d6c6a28be2965750",
435435
"is_secret": false,
436436
"is_verified": false,
437-
"line_number": 446,
437+
"line_number": 450,
438438
"type": "Secret Keyword",
439439
"verified_result": null
440440
},
441441
{
442442
"hashed_secret": "2863fa4b5510c46afc2bd2998dfbc0cf3d6df032",
443443
"is_secret": false,
444444
"is_verified": false,
445-
"line_number": 527,
445+
"line_number": 531,
446446
"type": "Secret Keyword",
447447
"verified_result": null
448448
},
449449
{
450450
"hashed_secret": "b9cad336062c0dc3bb30145b1a6697fccfe755a6",
451451
"is_secret": false,
452452
"is_verified": false,
453-
"line_number": 588,
453+
"line_number": 592,
454454
"type": "Secret Keyword",
455455
"verified_result": null
456456
}
@@ -460,15 +460,15 @@
460460
"hashed_secret": "c8f0df25bade89c1873f5f01b85bcfb921443ac6",
461461
"is_secret": false,
462462
"is_verified": false,
463-
"line_number": 29,
463+
"line_number": 32,
464464
"type": "JSON Web Token",
465465
"verified_result": null
466466
},
467467
{
468468
"hashed_secret": "f06e1073ca9afdd800a2cf27f944d06530b5b755",
469469
"is_secret": false,
470470
"is_verified": false,
471-
"line_number": 30,
471+
"line_number": 33,
472472
"type": "JSON Web Token",
473473
"verified_result": null
474474
}

README.md

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,66 @@ Before opening a new issue, please search for similar issues. It's possible that
3838

3939
## Logging
4040

41+
This library uses Python's built-in `logging` module to perform logging of error,
42+
warning, informational and debug messages.
43+
The components within the SDK Core library use a single logger named `ibm-cloud-sdk-core`.
44+
45+
For complete information on the logging facility, please see: [Logging facility for Python](https://docs.python.org/3/library/logging.html).
46+
4147
### Enable logging
4248

49+
There are various ways to configure and enable the logging facility.
50+
51+
The code example below demonstrates a simple way to enable debug logging by invoking
52+
the `logging.basicConfig()` function.
53+
54+
Note that, as a convenience, if you set the logging level to `DEBUG`, then HTTP request/response message logging
55+
is also enabled.
56+
57+
The following code example shows how debug logging can be enabled:
4358
```python
4459
import logging
45-
logging.basicConfig(level=logging.DEBUG)
46-
```
4760

48-
This would show output of the form:
49-
```
50-
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): iam.cloud.ibm.com:443
51-
DEBUG:urllib3.connectionpool:https://iam.cloud.ibm.com:443 "POST /identity/token HTTP/1.1" 200 1809
52-
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
53-
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "POST /assistant/api/v1/workspaces?version=2018-07-10 HTTP/1.1" 201 None
54-
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
55-
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "GET /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10&export=true HTTP/1.1" 200 None
56-
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): gateway.watsonplatform.net:443
57-
DEBUG:urllib3.connectionpool:https://gateway.watsonplatform.net:443 "DELETE /assistant/api/v1/workspaces/883a2a44-eb5f-4b1a-96b0-32a90b475ea8?version=2018-07-10 HTTP/1.1" 200 28
61+
# Create a basic logging configuration that:
62+
# 1. Defines a handler to display messages on the console.
63+
# 2. Sets the root logger's logging level to DEBUG.
64+
# 3. Sets the 'format' string used to display messages.
65+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(name)s:%(levelname)s] %(message)s', force=True)
5866
```
5967

60-
### Low level request and response dump
61-
To get low level information of the requests/ responses:
62-
63-
```python
64-
from http.client import HTTPConnection
65-
HTTPConnection.debuglevel = 1
68+
When running your application, you should see output like this if debug logging is enabled:
69+
```
70+
2024-09-16 15:44:45,174 [ibm-cloud-sdk-core:DEBUG] Get authenticator from environment, key=global_search
71+
2024-09-16 15:44:45,175 [ibm-cloud-sdk-core:DEBUG] Set service URL: https://api.global-search-tagging.cloud.ibm.com
72+
2024-09-16 15:44:45,175 [ibm-cloud-sdk-core:DEBUG] Set User-Agent: ibm-python-sdk-core-3.20.6 os.name=Linux os.version=6.10.9-100.fc39.x86_64 python.version=3.12.5
73+
2024-09-16 15:44:45,181 [ibm-cloud-sdk-core:DEBUG] Configuring BaseService instance with service name: global_search
74+
2024-09-16 15:44:45,181 [ibm-cloud-sdk-core:DEBUG] Performing synchronous token fetch
75+
2024-09-16 15:44:45,182 [ibm-cloud-sdk-core:DEBUG] Invoking IAM get_token operation: https://iam.cloud.ibm.com/identity/token
76+
2024-09-16 15:44:45,182 [urllib3.connectionpool:DEBUG] Starting new HTTPS connection (1): iam.cloud.ibm.com:443
77+
send: b'POST /identity/token HTTP/1.1\r\nHost: iam.cloud.ibm.com\r\nUser-Agent: ibm-python-sdk-core/iam-authenticator-3.20.6 os.name=Linux os.version=6.10.9-100.fc39.x86_64 python.version=3.12.5\r\nAccept-Encoding: gzip, deflate\r\nAccept: application/json\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 135\r\n\r\n'
78+
send: b'grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey=[redacted]&response_type=cloud_iam'
79+
reply: 'HTTP/1.1 200 OK\r\n'
80+
header: Content-Type: application/json
81+
header: Content-Language: en-US
82+
header: Content-Encoding: gzip
83+
header: Date: Mon, 16 Sep 2024 20:44:45 GMT
84+
header: Content-Length: 983
85+
header: Connection: keep-alive
86+
2024-09-16 15:44:45,670 [urllib3.connectionpool:DEBUG] https://iam.cloud.ibm.com:443 "POST /identity/token HTTP/11" 200 983
87+
2024-09-16 15:44:45,672 [ibm-cloud-sdk-core:DEBUG] Returned from IAM get_token operation
88+
2024-09-16 15:44:45,673 [ibm-cloud-sdk-core:DEBUG] Authenticated outbound request (type=iam)
89+
2024-09-16 15:44:45,673 [ibm-cloud-sdk-core:DEBUG] Prepared request [POST https://api.global-search-tagging.cloud.ibm.com/v3/resources/search]
90+
2024-09-16 15:44:45,673 [ibm-cloud-sdk-core:DEBUG] Sending HTTP request message
91+
2024-09-16 15:44:45,674 [urllib3.connectionpool:DEBUG] Starting new HTTPS connection (1): api.global-search-tagging.cloud.ibm.com:443
92+
send: b'POST /v3/resources/search?limit=1 HTTP/1.1\r\nHost: api.global-search-tagging.cloud.ibm.com\r\nUser-Agent: platform-services-python-sdk/0.57.0 (lang=python; os.name=Linux; os.version=6.10.9-100.fc39.x86_64; python.version=3.12.5)\r\nAccept-Encoding: gzip, deflate\r\nAccept: application/json\r\nConnection: keep-alive\r\ncontent-type: application/json\r\nAuthorization: [redacted]\r\nContent-Length: 39\r\n\r\n'
93+
send: b'{"query": "GST-sdk-*", "fields": ["*"]}'
94+
reply: 'HTTP/1.1 200 OK\r\n'
95+
header: Content-Type: application/json
96+
header: Content-Length: 22
97+
header: Date: Mon, 16 Sep 2024 20:44:46 GMT
98+
header: Connection: keep-alive
99+
2024-09-16 15:44:46,079 [urllib3.connectionpool:DEBUG] https://api.global-search-tagging.cloud.ibm.com:443 "POST /v3/resources/search?limit=1 HTTP/11" 200 22
100+
2024-09-16 15:44:46,080 [ibm-cloud-sdk-core:DEBUG] Received HTTP response message, status code 200
66101
```
67102

68103
## Open source @ IBM

ibm_cloud_sdk_core/authenticators/basic_authenticator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# Copyright 2019 IBM All Rights Reserved.
3+
# Copyright 2019, 2024 IBM All Rights Reserved.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -15,11 +15,15 @@
1515
# limitations under the License.
1616

1717
import base64
18+
1819
from requests import Request
1920

21+
from ibm_cloud_sdk_core.logger import get_logger
2022
from .authenticator import Authenticator
2123
from ..utils import has_bad_first_or_last_char
2224

25+
logger = get_logger()
26+
2327

2428
class BasicAuthenticator(Authenticator):
2529
"""The BasicAuthenticator is used to add basic authentication information to requests.
@@ -41,6 +45,7 @@ def __init__(self, username: str, password: str) -> None:
4145
self.password = password
4246
self.validate()
4347
self.authorization_header = self.__construct_basic_auth_header()
48+
logger.debug('Created new BasicAuthenticator instance!')
4449

4550
def authentication_type(self) -> str:
4651
"""Returns this authenticator's type ('basic')."""
@@ -81,3 +86,4 @@ def authenticate(self, req: Request) -> None:
8186
"""
8287
headers = req.get('headers')
8388
headers['Authorization'] = self.authorization_header
89+
logger.debug('Authenticated outbound request (type=%s)', self.authentication_type())

0 commit comments

Comments
 (0)