Commit a4da13a
REFACTOR: Enhance & Fix Logs (#137)
### ADO Work Item Reference
<!-- Insert your ADO Work Item ID below (e.g. AB#37452) -->
>
[AB#37336](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37336)
-------------------------------------------------------------------
### Summary
<!-- Insert your Copilot Generated Summary below -->
This pull request refactors the logging system in the `mssql_python`
package, replacing the previous global logging mechanism with a
centralized `LoggingManager` class. It also introduces a universal
logging helper function (`log`) and a utility to sanitize sensitive
information in connection strings. Additionally, it simplifies the
codebase by removing redundant logging checks and improving resource
management for cursors and connections.
### Logging System Refactor:
* Introduced `LoggingManager` as a singleton class to manage logging
configuration, replacing the global `ENABLE_LOGGING` variable. The class
centralizes logging setup and provides backward compatibility for
checking if logging is enabled. (`mssql_python/logging_config.py`,
[mssql_python/logging_config.pyR11-R52](diffhunk://#diff-36f7c1765dd718a86de00178366ca7ab8e503766ca04b601e859d943d82a292dR11-R52))
* Added a universal `log` function in `helpers.py` to streamline logging
calls across the codebase. This function dynamically retrieves a logger
instance and supports multiple log levels. (`mssql_python/helpers.py`,
[mssql_python/helpers.pyR187-R214](diffhunk://#diff-7e002427036075c912554bb92d9e19681ea477e7c2a0eec56d284934d9125343R187-R214))
### Code Simplification:
* Replaced conditional `ENABLE_LOGGING` checks with calls to the new
`log` function, simplifying logging logic in `connection.py`,
`cursor.py`, and `exceptions.py`. (`mssql_python/connection.py`,
[[1]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90L129-R125);
`mssql_python/cursor.py`,
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L465-R449);
`mssql_python/exceptions.py`,
[[3]](diffhunk://#diff-261e8fab7c29f0ba7f4dcdd1f977df5c9f02d3423c86cc935c3f617fd15c2d5fL644-R644)
* Removed redundant `logger` initialization from several modules, as the
`log` function now handles logger retrieval. (`mssql_python/auth.py`,
[[1]](diffhunk://#diff-19a0c93fc8573a5a7bfcadda0a2fb8f1b340c4502e1308c4f8a1e4508136c6e1L10-L14);
`mssql_python/cursor.py`,
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L17-L23)
### New Utilities:
* Added `sanitize_connection_string` in `helpers.py` to mask sensitive
information (e.g., passwords) in connection strings before logging.
(`mssql_python/helpers.py`,
[mssql_python/helpers.pyR187-R214](diffhunk://#diff-7e002427036075c912554bb92d9e19681ea477e7c2a0eec56d284934d9125343R187-R214))
### Resource Management Improvements:
* Improved cursor tracking by explicitly adding cursors to a
connection's cursor set and ensuring proper cleanup during resource
deallocation. (`mssql_python/connection.py`,
[mssql_python/connection.pyR186](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90R186))
* Refactored cursor cleanup logic to avoid unnecessary operations and
ensure weak references are handled correctly. (`mssql_python/cursor.py`,
[mssql_python/cursor.pyL419-R432](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L419-R432))
### Minor Enhancements:
* Updated `__del__` methods in `connection.py` and `cursor.py` to handle
exceptions gracefully during cleanup, avoiding issues during garbage
collection. (`mssql_python/connection.py`,
[[1]](diffhunk://#diff-29bb94de45aae51c23a6426d40133c28e4161e68769e08d046059c7186264e90L273-R285);
`mssql_python/cursor.py`,
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L750-R737)
<!--
### PR Title Guide
> For feature requests
FEAT: (short-description)
> For non-feature requests like test case updates, config updates ,
dependency updates etc
CHORE: (short-description)
> For Fix requests
FIX: (short-description)
> For doc update requests
DOC: (short-description)
> For Formatting, indentation, or styling update
STYLE: (short-description)
> For Refactor, without any feature changes
REFACTOR: (short-description)
> For release related changes, without any feature changes
RELEASE: #<RELEASE_VERSION> (short-description)
-->
---------
Co-authored-by: Jahnvi Thakkar <61936179+jahnvi480@users.noreply.github.com>1 parent 2dac682 commit a4da13a
File tree
11 files changed
+411
-147
lines changed- mssql_python
- pybind
- tests
11 files changed
+411
-147
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | | - | |
14 | | - | |
15 | 12 | | |
16 | 13 | | |
17 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
| 16 | + | |
19 | 17 | | |
20 | 18 | | |
21 | | - | |
| 19 | + | |
22 | 20 | | |
23 | 21 | | |
24 | | - | |
25 | | - | |
26 | 22 | | |
27 | 23 | | |
28 | 24 | | |
| |||
126 | 122 | | |
127 | 123 | | |
128 | 124 | | |
129 | | - | |
130 | | - | |
| 125 | + | |
131 | 126 | | |
132 | 127 | | |
133 | 128 | | |
| |||
150 | 145 | | |
151 | 146 | | |
152 | 147 | | |
153 | | - | |
154 | | - | |
| 148 | + | |
155 | 149 | | |
156 | 150 | | |
157 | 151 | | |
| |||
189 | 183 | | |
190 | 184 | | |
191 | 185 | | |
| 186 | + | |
192 | 187 | | |
193 | 188 | | |
194 | 189 | | |
| |||
205 | 200 | | |
206 | 201 | | |
207 | 202 | | |
208 | | - | |
209 | | - | |
| 203 | + | |
210 | 204 | | |
211 | 205 | | |
212 | 206 | | |
| |||
221 | 215 | | |
222 | 216 | | |
223 | 217 | | |
224 | | - | |
225 | | - | |
| 218 | + | |
226 | 219 | | |
227 | 220 | | |
228 | 221 | | |
| |||
246 | 239 | | |
247 | 240 | | |
248 | 241 | | |
249 | | - | |
| 242 | + | |
250 | 243 | | |
251 | 244 | | |
252 | 245 | | |
253 | 246 | | |
254 | 247 | | |
255 | 248 | | |
256 | 249 | | |
257 | | - | |
258 | | - | |
| 250 | + | |
259 | 251 | | |
260 | 252 | | |
261 | | - | |
262 | | - | |
| 253 | + | |
| 254 | + | |
263 | 255 | | |
264 | 256 | | |
265 | 257 | | |
| |||
270 | 262 | | |
271 | 263 | | |
272 | 264 | | |
273 | | - | |
274 | | - | |
| 265 | + | |
275 | 266 | | |
276 | 267 | | |
277 | 268 | | |
278 | 269 | | |
279 | 270 | | |
280 | 271 | | |
281 | | - | |
282 | | - | |
| 272 | + | |
283 | 273 | | |
284 | 274 | | |
285 | 275 | | |
286 | 276 | | |
287 | 277 | | |
288 | 278 | | |
289 | 279 | | |
290 | | - | |
| 280 | + | |
291 | 281 | | |
292 | 282 | | |
293 | 283 | | |
294 | | - | |
295 | | - | |
| 284 | + | |
| 285 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
19 | 18 | | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
| |||
415 | 414 | | |
416 | 415 | | |
417 | 416 | | |
418 | | - | |
419 | 417 | | |
420 | | - | |
421 | | - | |
422 | 418 | | |
423 | 419 | | |
424 | 420 | | |
425 | 421 | | |
426 | 422 | | |
427 | 423 | | |
428 | 424 | | |
429 | | - | |
| 425 | + | |
430 | 426 | | |
431 | | - | |
| 427 | + | |
432 | 428 | | |
433 | 429 | | |
434 | 430 | | |
435 | 431 | | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
| 432 | + | |
448 | 433 | | |
449 | 434 | | |
450 | 435 | | |
| |||
461 | 446 | | |
462 | 447 | | |
463 | 448 | | |
464 | | - | |
465 | | - | |
| 449 | + | |
466 | 450 | | |
467 | 451 | | |
468 | 452 | | |
| |||
596 | 580 | | |
597 | 581 | | |
598 | 582 | | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
608 | 591 | | |
609 | 592 | | |
610 | 593 | | |
| |||
709 | 692 | | |
710 | 693 | | |
711 | 694 | | |
712 | | - | |
713 | | - | |
714 | | - | |
715 | | - | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
716 | 698 | | |
717 | 699 | | |
718 | 700 | | |
| |||
784 | 766 | | |
785 | 767 | | |
786 | 768 | | |
| 769 | + | |
787 | 770 | | |
788 | 771 | | |
789 | 772 | | |
| |||
805 | 788 | | |
806 | 789 | | |
807 | 790 | | |
808 | | - | |
| 791 | + | |
809 | 792 | | |
810 | 793 | | |
811 | 794 | | |
812 | 795 | | |
813 | 796 | | |
814 | 797 | | |
815 | | - | |
| 798 | + | |
816 | 799 | | |
817 | 800 | | |
818 | 801 | | |
819 | | - | |
| 802 | + | |
| 803 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
621 | 621 | | |
622 | 622 | | |
623 | 623 | | |
624 | | - | |
| 624 | + | |
625 | 625 | | |
626 | 626 | | |
627 | 627 | | |
| |||
641 | 641 | | |
642 | 642 | | |
643 | 643 | | |
644 | | - | |
| 644 | + | |
645 | 645 | | |
646 | 646 | | |
647 | 647 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
0 commit comments