Commit c07cbfa
authored
FEAT: mssql-python driver changes to support bulk copy logging. (#430)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
AB#42113
-------------------------------------------------------------------
### Summary
This pull request introduces significant improvements to the logging
system in the `mssql_python` package, focusing on performance
optimization, better integration with Rust (`py-core`), and enhanced
test coverage. The changes optimize how logging levels are checked,
ensure that logging from Rust code is handled correctly, and add
thorough tests for these new features.
**Key changes:**
### Logging Performance and Behavior Improvements
- Switched the default logging level from `CRITICAL` to `WARNING`,
ensuring that warnings and errors are visible by default without
explicit configuration. Also, a default stderr handler is now added for
warnings and errors.
[[1]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28L91-R91)
[[2]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28L107-R122)
- Introduced cached logging level checks (`_cached_level` and
`_is_debug_enabled`) for fast, low-overhead decision-making in logging
calls, with a new `is_debug_enabled` property for quick checks.
[[1]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28L107-R122)
[[2]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28R505-R515)
[[3]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28R621-R625)
- Updated all logging calls in `_bulkcopy` (in `cursor.py`) to use the
new fast check for debug logging, and to avoid unnecessary logger
passing to Rust unless logging is enabled.
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R2577-R2591)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L2691-R2706)
[[3]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R2719-R2727)
### Rust (`py-core`) Logging Integration
- Added a new `py_core_log` method to the logger, allowing Rust-side
code to emit logs with custom source locations and levels, and ensuring
these logs are formatted and filtered consistently with Python logs.
- Enhanced the CSV log formatter to recognize and format logs
originating from `py-core` distinctly.
### Robustness and Error Handling
- Ensured that logging always allows `WARNING` and `ERROR` messages,
even when the logger is otherwise disabled, both in Python and
Rust-originated logs.
[[1]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28L355-R416)
[[2]](diffhunk://#diff-a8f9a63854a2b8541d3d9260e97948990dba646ca62b8e0d135055fd7f543f28R349-R386)
- Improved error logging in `_bulkcopy` for various failure cases, such
as missing dependencies or invalid parameters.
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R2577-R2591)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R2623)
[[3]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R2633-R2637)
### Test Suite Enhancements
- Added comprehensive tests for the new `is_debug_enabled` property and
for `py_core_log`, covering level filtering, custom source locations,
CSV formatting, error fallback, and integration with different Rust
files.
- Updated test cleanup utilities to maintain consistency with the new
cached logging level logic.
[[1]](diffhunk://#diff-39a609c16b2d72fabefaf23a0f99dccae8bd4bf5e481928bdeaf53d6131a0fd4R34-R35)
[[2]](diffhunk://#diff-39a609c16b2d72fabefaf23a0f99dccae8bd4bf5e481928bdeaf53d6131a0fd4R51-R52)
These changes collectively improve logging performance, reliability, and
cross-language integration, while ensuring robust test coverage for the
new features.
**High Level Design**
<img width="426" height="986" alt="image"
src="https://github.com/user-attachments/assets/be4ddc70-7974-484e-9ad0-22e17321e16d"
/>
**Success Test Log**
<img width="1790" height="952" alt="image"
src="https://github.com/user-attachments/assets/073a07e1-3235-4587-a747-274e5aa06113"
/>
**Failure Test Log**
<img width="1887" height="957" alt="image"
src="https://github.com/user-attachments/assets/8d39e902-1f13-4cce-a252-2dc12deb94ac"
/>1 parent ba369c7 commit c07cbfa
3 files changed
+302
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2575 | 2575 | | |
2576 | 2576 | | |
2577 | 2577 | | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
2578 | 2581 | | |
2579 | 2582 | | |
2580 | 2583 | | |
| 2584 | + | |
2581 | 2585 | | |
2582 | 2586 | | |
2583 | 2587 | | |
2584 | 2588 | | |
2585 | 2589 | | |
2586 | 2590 | | |
2587 | 2591 | | |
| 2592 | + | |
2588 | 2593 | | |
2589 | 2594 | | |
2590 | 2595 | | |
| |||
2616 | 2621 | | |
2617 | 2622 | | |
2618 | 2623 | | |
| 2624 | + | |
2619 | 2625 | | |
2620 | 2626 | | |
2621 | 2627 | | |
| |||
2652 | 2658 | | |
2653 | 2659 | | |
2654 | 2660 | | |
2655 | | - | |
| 2661 | + | |
| 2662 | + | |
| 2663 | + | |
| 2664 | + | |
2656 | 2665 | | |
2657 | 2666 | | |
| 2667 | + | |
| 2668 | + | |
2658 | 2669 | | |
2659 | 2670 | | |
2660 | 2671 | | |
| |||
2667 | 2678 | | |
2668 | 2679 | | |
2669 | 2680 | | |
| 2681 | + | |
| 2682 | + | |
| 2683 | + | |
| 2684 | + | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
2670 | 2689 | | |
2671 | 2690 | | |
2672 | 2691 | | |
| |||
2694 | 2713 | | |
2695 | 2714 | | |
2696 | 2715 | | |
2697 | | - | |
2698 | | - | |
| 2716 | + | |
2699 | 2717 | | |
2700 | 2718 | | |
2701 | 2719 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
108 | 123 | | |
109 | 124 | | |
110 | 125 | | |
| |||
145 | 160 | | |
146 | 161 | | |
147 | 162 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
154 | 167 | | |
155 | | - | |
156 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
157 | 177 | | |
158 | 178 | | |
159 | 179 | | |
| |||
326 | 346 | | |
327 | 347 | | |
328 | 348 | | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
329 | 387 | | |
330 | 388 | | |
331 | 389 | | |
| |||
352 | 410 | | |
353 | 411 | | |
354 | 412 | | |
355 | | - | |
356 | | - | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
357 | 417 | | |
358 | 418 | | |
359 | 419 | | |
| |||
364 | 424 | | |
365 | 425 | | |
366 | 426 | | |
367 | | - | |
368 | | - | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
369 | 430 | | |
370 | 431 | | |
371 | 432 | | |
| |||
441 | 502 | | |
442 | 503 | | |
443 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
444 | 516 | | |
445 | 517 | | |
446 | 518 | | |
| |||
546 | 618 | | |
547 | 619 | | |
548 | 620 | | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
549 | 626 | | |
550 | 627 | | |
551 | 628 | | |
| |||
0 commit comments