Logging
s3lst_ds.utilities.logging_utils
Classes:
| Name | Description |
|---|---|
RichLogger |
A |
Functions:
| Name | Description |
|---|---|
create_file_handler |
Create a logging |
create_rich_handler |
Create a customized logging |
get_rich_text_from_renderable |
Capture the |
get_terminal_console |
Get Rich |
RichLogger
A logging.Logger-based class that uses a Rich console and that may also write to
file.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str, default="root"
|
Name of the base logger. If a name was not issued, the base root logger (of name
|
level |
{50, 40, 30, 20, 10, 0}
|
Minimum logging
level of the
base logger. Defaults to the level of root ( |
log_mode |
{None, "console", "file", "both"}, default="both"
|
Mode for logging:
- |
rich_handler |
RichHandler or None
|
The Rich console logging handler used by the base logger. If |
file_handlers |
dict[Path, FileHandler]
|
File logging handlers used by the base logger if any, keyed by by the paths to
the respective files ( |
file_paths |
list[Path]
|
Paths to the logging files (regardless of the handlers being set or not). |
file_modes |
dict[Path, str]
|
Modes for file logging (regardless of the handlers being set or not) (e.g. |
base_logger |
Logger
|
The base logger. |
base_warnings_logger |
Logger
|
A logger for the warnings of the warning package. This logger shares the same handlers as the base logger. |
console |
Console
|
The Rich console (regardless of the handler being set or not). |
level_mapper |
dict[int or str or None, int or None]
|
Class-level mapping between logging level aliases and their corresponding
numeric levels. Numeric levels are mapped to themselves, logging level names are
mapped to their corresponding numeric levels, and |
Methods:
| Name | Description |
|---|---|
__getstate__ |
Get dictionary storing instance's picklable parameters (all except |
__init__ |
Initialize instance by getting a base logger with the issued |
__setstate__ |
Set instance's picklable parameters from the issued |
add_handler |
Add the issued logging |
critical |
Wrapper for base logger critical method. |
debug |
Wrapper for base logger debug method. |
drop_all_file_handlers |
Drop all file logging handlers from the base logger if they exist. |
drop_all_handlers |
Drop all logging handlers from the base logger if they exist. |
drop_all_rich_handlers |
Drop all Rich logging handlers from the base logger if they exist. |
drop_handler |
Drop the logging handler with the issued |
drop_other_rich_handlers |
Drop all Rich logging handlers from the base logger except the first one. |
error |
Wrapper for base logger error method. |
exception |
Wrapper for base logger exception method. |
fatal |
Wrapper for base logger fatal method. |
get_base_logger |
Get the base logger. |
get_file_handler_modes |
Get the modes of file logging handlers of the base logger if they exist, keyed |
get_file_handler_paths |
Get the paths of file logging handlers of the base logger if they exist. |
has_handler |
Check if the base logger has a logging handler with the issued |
info |
Wrapper for base logger info method. |
set_file_handler |
Set file logging handlers in the base logger that writes to files at |
set_file_handler_single |
Set a file logging handler in the base logger that writes to file at |
set_rich_handler |
Set the issued Rich console in the base logger by removing the respective Rich |
stop_propagation |
Stop information propagation to the base root logger to avoid double logging. |
warning |
Wrapper for base logger warning method. |
Source code in src/s3lst_ds/utilities/logging_utils.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |
base_warnings_logger
instance-attribute
base_warnings_logger: Logger = logging.getLogger('py.warnings')
A logger for the warnings of the warning package. This logger shares the same handlers as the base logger.
console
property
Get the Rich console used by the base logger.
Returns:
| Name | Type | Description |
|---|---|---|
console |
Console or None
|
The Rich console used by the base logger, if it exists. |
file_handlers
property
Get file logging handlers of the base logger if they exist.
Returns:
| Name | Type | Description |
|---|---|---|
file_handlers |
dict[Path, FileHandler]
|
File logging handlers of the base logger if they exist, keyed by their file paths. If they do not exist, an empty dict is returned. |
file_modes
property
Get the file logging modes.
Returns:
| Name | Type | Description |
|---|---|---|
file_modes |
dict[Path, str]
|
Modes for file logging (e.g. |
file_paths
property
Get the file logging paths.
Returns:
| Name | Type | Description |
|---|---|---|
file_paths |
list[Path]
|
Paths to the logging files. |
level
property
writable
Get the minimum logging level of the base logger.
Returns:
| Name | Type | Description |
|---|---|---|
level |
{50, 40, 30, 20, 10, 0}
|
The minimum logging level of the base logger. |
level_mapper
class-attribute
level_mapper: dict[int | str | None, int | None] = {
0: 0,
10: 10,
20: 20,
30: 30,
40: 40,
50: 50,
"notset": 0,
"debug": 10,
"info": 20,
"warning": 30,
"error": 40,
"critical": 50,
None: None,
}
log_mode
property
writable
Get the logging mode of the logger.
Returns:
| Name | Type | Description |
|---|---|---|
log_mode |
{None, console, file, both}
|
Mode for logging:
- |
name
property
Get the name of the base logger.
Returns:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the base logger. |
rich_handler
property
Get the Rich logging handler of the base logger, if it exists.
Returns:
| Name | Type | Description |
|---|---|---|
rich_handler |
RichHandler or None
|
The Rich logging handler of the base logger, if it exists. |
__getstate__
Get dictionary storing instance's picklable parameters (all except console).
Returns:
| Name | Type | Description |
|---|---|---|
state |
dict
|
Dictionary with instance's picklable parameters (all except |
Source code in src/s3lst_ds/utilities/logging_utils.py
__init__
__init__(
name: str | None = None,
level: Literal[
50, 40, 30, 20, 10, 0, "critical", "error", "warning", "info", "debug", "notset"
]
| None = None,
console: Console | None = None,
file_path: Path | None = None,
file_mode: str = "w",
log_mode: Literal["console", "file", "both"] | None = "both",
) -> None
issued console as well as a file logging handler that writes to file at
file_path considering the issued file_mode. Note that Rich console logging
is only considered if log_mode corresponds to "console" or "both".
Furthermore, file logging is only considered if log_mode corresponds to
"file" or "both".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str or None
|
Name of the base logger. If issued and a base logger with such name already
exists, it will be used and updated instead of recreated from scratch. If
not issued, the base root logger (called |
None
|
level
|
{50, 40, 30, 20, 10, 0, "critical", "error", "warning", "info", "debug",
|
"notset", None}, default=None
Minimum logging
level (e.g.
|
None
|
console
|
Console | None
|
A Rich console object to use for console logging. Regardless of whether the
base logger already exists or not, if a console is issued, it will be set in
the base logger. If not issued and the base logger already exists and
contains a Rich console, this latter will be kept. If it does not contain, a
new Rich console is set. Note that if the base logger contains multiple Rich
console handlers, only the first one is considered. Also, note that a Rich
console handler is only considered if |
None
|
file_path
|
Path or None
|
Path to file for logging. If not issued, no new file logging is set up. Note
that file logging is solely considered if |
None
|
file_mode
|
str
|
Mode for file logging (e.g. |
"w"
|
log_mode
|
(None, console, file, both)
|
Mode for logging:
- |
None
|
Source code in src/s3lst_ds/utilities/logging_utils.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 | |
__setstate__
Set instance's picklable parameters from the issued state dictionary and
recreate unpicklable parameters (console).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict
|
Dictionary with instance's picklable parameters (all except |
required |
Source code in src/s3lst_ds/utilities/logging_utils.py
add_handler
Add the issued logging handler to the base logger by firstly dropping any
existing handler with the same name (if handler has a name).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
Handler
|
The handler to add. |
required |
Source code in src/s3lst_ds/utilities/logging_utils.py
critical
debug
drop_all_file_handlers
Drop all file logging handlers from the base logger if they exist.
Source code in src/s3lst_ds/utilities/logging_utils.py
drop_all_handlers
Drop all logging handlers from the base logger if they exist.
Source code in src/s3lst_ds/utilities/logging_utils.py
drop_all_rich_handlers
Drop all Rich logging handlers from the base logger if they exist.
Source code in src/s3lst_ds/utilities/logging_utils.py
drop_handler
Drop the logging handler with the issued name from the base logger if it
exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the handler to drop. |
required |
Source code in src/s3lst_ds/utilities/logging_utils.py
drop_other_rich_handlers
Drop all Rich logging handlers from the base logger except the first one.
Source code in src/s3lst_ds/utilities/logging_utils.py
error
exception
fatal
get_base_logger
Get the base logger.
Returns:
| Name | Type | Description |
|---|---|---|
base_logger |
Logger
|
The base logger. |
get_file_handler_modes
Get the modes of file logging handlers of the base logger if they exist, keyed by their file paths.
Returns:
| Name | Type | Description |
|---|---|---|
file_handler_modes |
dict[Path, str]
|
Modes of file logging handlers of the base logger if they exist, keyed by their file paths. If they do not exist, an empty dict is returned. |
Source code in src/s3lst_ds/utilities/logging_utils.py
get_file_handler_paths
Get the paths of file logging handlers of the base logger if they exist.
Returns:
| Name | Type | Description |
|---|---|---|
file_handler_paths |
list[Path]
|
Paths of file logging handlers of the base logger if they exist. If they do not exist, an empty list is returned. |
Source code in src/s3lst_ds/utilities/logging_utils.py
has_handler
Check if the base logger has a logging handler with the issued name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the handler to check for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
has_handler |
bool
|
Whether the base logger has a handler with the issued name. |
Source code in src/s3lst_ds/utilities/logging_utils.py
info
set_file_handler
Set file logging handlers in the base logger that writes to files at
file_paths considering the issued file_modes. Further create parent folders
for each file_path in file_paths and set file_path.as_posix() as the name
of the respective handler. Note that file logging is solely considered if
log_mode corresponds to "file" or "both".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
Path or list[Path] or None
|
Paths to files for logging. If not issued, no new file logging is set up.
Note that file logging is solely considered if |
required |
file_modes
|
str or dict[Path, str]
|
Modes for file logging (e.g. |
required |
Source code in src/s3lst_ds/utilities/logging_utils.py
set_file_handler_single
Set a file logging handler in the base logger that writes to file at file_path
considering the issued file_mode. Further create parent folders for the log
file if they do not exist, and set file_path.as_posix() as the name of the
handler. Note that file logging is solely considered if log_mode corresponds
to "file" or "both".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path or None
|
Path to file for logging. If not issued, no new file logging is set up. Note
that file logging is solely considered if |
required |
file_mode
|
str
|
Mode for file logging (e.g. |
"w"
|
Source code in src/s3lst_ds/utilities/logging_utils.py
set_rich_handler
Set the issued Rich console in the base logger by removing the respective Rich
logging handler if it exists, and creating a new one from scratch. If not
issued, the Rich logging handler will not be changed if it exists, or a new
console will be used and the handler created with it if it does not exist. Note
that a Rich console handler is only considered if log_mode corresponds to
"console" or "both".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
console
|
Console or None
|
The Rich console to use in the logging handler. |
None
|
Source code in src/s3lst_ds/utilities/logging_utils.py
stop_propagation
Stop information propagation to the base root logger to avoid double logging.
Source code in src/s3lst_ds/utilities/logging_utils.py
create_file_handler
Create a logging FileHandler with the issued file_path and file_mode. Further
create parent folders for the log file if they do not exist, and set
file_path.as_posix() as the name of the handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Path or None
|
Path to file for logging. |
required |
file_mode
|
str
|
Mode for file logging (e.g. |
"w"
|
Returns:
| Name | Type | Description |
|---|---|---|
file_handler |
FileHandler
|
The created file logging handler. |
Source code in src/s3lst_ds/utilities/logging_utils.py
create_rich_handler
Create a customized logging RichHandler with the issued console. Further set the
name of the handler to "rich".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
console
|
Console
|
The Rich |
required |
Returns:
| Name | Type | Description |
|---|---|---|
rich_handler |
RichHandler
|
The created Rich logging handler. |
Source code in src/s3lst_ds/utilities/logging_utils.py
get_rich_text_from_renderable
Capture the console output of the renderable object (without printing it to
terminal) and return it as a Text object.
For more details, see https://github.com/Textualize/rich/discussions/1799#discussioncomment-1994605.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
console
|
Console
|
The Rich console to use for capturing the renderable object output. |
required |
renderable
|
RenderableType
|
The Rich renderable object output to capture. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
text |
Text
|
The captured renderable object output as a Rich |
Source code in src/s3lst_ds/utilities/logging_utils.py
get_terminal_console
Get Rich Console object with forced terminal mode (when possible) for better
logging and display in both terminal and Jupyter notebook environments. The console
may be either a global one or a new one depending on the issued use_global
argument. In the case of the former, direct enforcing of the terminal mode cannot be
done, but argument is_jupyter is set to False as in case of the latter.
Also set console width to 88 characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_global
|
bool
|
Whether to use a global console or to create a new one. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
console |
Console
|
A Rich |