colored_logger
colored_logger ¶
Colored logger wrapper that automatically applies colors to log levels.
This module provides a ColoredLogger class that wraps cl and automatically applies appropriate colors to different log levels for better visual distinction in console output.
Quick Start for Users and AI Agents¶
Standard Usage (Recommended):
from crew_dcs.utils.logging import get_colored_logger
# Get a logger with default settings (INFO level, colored output)
logger = get_colored_logger()
# Use in async functions
await logger.info("Operation completed")
await logger.warning("This might be a problem")
await logger.error("Something went wrong")
Configuration for Scripts/Notebooks:
from crew_dcs.utils.logging import set_domolibrary_logger
# Configure logger with custom settings
logger = set_domolibrary_logger(
app_name="my_script",
min_level="DEBUG", # Show DEBUG, INFO, WARNING, ERROR, CRITICAL
set_as_global=True, # Make this the global logger for all modules
)
Adjust Level Dynamically:
# Show more verbose output
logger.set_level("DEBUG")
# Show only warnings and errors
logger.set_level("WARNING")
# Check current level
current_level = logger.get_level() # Returns 'DEBUG', 'INFO', etc.
For Route Functions (API calls):
from crew_dcs.utils.logging import log_call, LogDecoratorConfig
@log_call(
level_name="route",
config=LogDecoratorConfig(
entity_extractor=DomoEntityExtractor(),
result_processor=DomoEntityResultProcessor(),
),
)
async def my_route_function(auth, **kwargs):
# Function automatically logs entry/exit with colored output
pass
Level Hierarchy: - DEBUG (10) - Most verbose, shows everything - INFO (20) - Normal operations (default) - WARNING (30) - Warning messages only - ERROR (40) - Error messages only - CRITICAL (50) - Most severe only
Important Notes:
- ALWAYS import from crew_dcs.utils.logging, NEVER from cl directly
- NEVER import cl in crew_dcs code
- Logger methods are async: use await logger.info(), not logger.info()
- Use set_domolibrary_logger() at script entry points for global configuration
- Use get_colored_logger() within modules for local logger instances
ColoredLogger ¶
ColoredLogger(
base_logger: CrewLogger,
debug_color: str = "cyan",
info_color: str = "green",
warning_color: str = "yellow",
error_color: str = "bold_red",
critical_color: str = "bold_red",
min_level: str = "INFO",
exclude_patterns: list[str] | None = None,
)
Bases: CrewLogger
Logger that automatically colorizes messages by log level.
This class wraps cl's Logger and adds automatic coloring and level filtering. It's the standard logger for all crew_dcs code.
Default colors
- DEBUG: cyan
- INFO: green
- WARNING: yellow
- ERROR: red
- CRITICAL: bold_red
Usage
from crew_dcs.utils.logging import get_colored_logger
Basic usage¶
logger = get_colored_logger() await logger.info("This will be green") await logger.warning("This will be yellow") await logger.error("This will be red")
Control verbosity¶
logger.set_level("DEBUG") # Show all messages logger.set_level("WARNING") # Show only warnings and errors
Check current level¶
print(f"Current level: {logger.get_level()}")
Note
- All log methods are ASYNC - must use
await - Level filtering happens at this layer before passing to cl
- Use
set_domolibrary_logger()for global configuration - Use
get_colored_logger()for module-level instances
Initialize colored logger wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_logger
|
CrewLogger
|
The underlying cl Logger instance |
required |
debug_color
|
str
|
Color for debug messages (default: cyan) |
'cyan'
|
info_color
|
str
|
Color for info messages (default: green) |
'green'
|
warning_color
|
str
|
Color for warning messages (default: yellow) |
'yellow'
|
error_color
|
str
|
Color for error messages (default: bold_red) |
'bold_red'
|
critical_color
|
str
|
Color for critical messages (default: bold_red) |
'bold_red'
|
min_level
|
str
|
Minimum log level to display (default: INFO) |
'INFO'
|
exclude_patterns
|
list[str] | None
|
List of message patterns to filter out (default: None) |
None
|
Source code in src/crew_dcs/utils/logging/colored_logger.py
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 | |
critical
async
¶
critical(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any
) -> bool
Log CRITICAL level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | |
debug
async
¶
debug(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any
) -> bool
Log DEBUG level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
error
async
¶
error(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any
) -> bool
Log ERROR level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
get_level ¶
get_level() -> str
Get the current minimum log level.
Returns:
| Type | Description |
|---|---|
str
|
Current log level name (e.g., 'INFO', 'WARNING', 'ERROR') |
Example
logger = get_colored_logger() logger.get_level() 'INFO'
Source code in src/crew_dcs/utils/logging/colored_logger.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
info
async
¶
info(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any
) -> bool
Log INFO level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
log
async
¶
log(level: Any, message: str, **context: Any) -> bool
Log with automatic coloring for cl log() usage.
Source code in src/crew_dcs/utils/logging/colored_logger.py
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 | |
set_level ¶
set_level(level: str)
Set the minimum log level for this logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Log level name - 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' |
required |
Example
logger = get_colored_logger() logger.set_level('WARNING') # Only show WARNING and above logger.set_level('ERROR') # Only show ERROR and CRITICAL logger.set_level('INFO') # Show INFO and above (default)
Source code in src/crew_dcs/utils/logging/colored_logger.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
warning
async
¶
warning(
message: str,
method: str = "COMMENT",
level_name: str | None = None,
color: str | None = None,
**context: Any
) -> bool
Log WARNING level message with automatic coloring.
Source code in src/crew_dcs/utils/logging/colored_logger.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
LogDecoratorConfig
dataclass
¶
LogDecoratorConfig(
entity_extractor: Any = None,
result_processor: Any = None,
)
Compatibility shim for LogDecoratorConfig.
colorize ¶
colorize(message: str, color: str | None = None) -> str
Apply ANSI color codes to a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to colorize |
required |
color
|
str | None
|
Color name (e.g., 'red', 'green', 'cyan', 'bold_red') |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Colorized message string |
Source code in src/crew_dcs/utils/logging/colored_logger.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
get_colored_logger ¶
get_colored_logger(
app_name: str | None = None,
debug_color: str = "cyan",
info_color: str = "green",
warning_color: str = "yellow",
error_color: str = "bold_red",
critical_color: str = "bold_red",
env: str = "development",
exclude_patterns: list[str] | None = None,
min_level: str | None = None,
enable_datadog: bool | None = None,
set_as_global: bool = False,
force: bool = False,
) -> ColoredLogger
Get or create a colored logger instance with advanced features.
Standard Usage (Module-Level): Call with no parameters to get the existing global logger. This is the recommended pattern for module-level logger variables: >>> logger = get_colored_logger() # Gets existing global logger
Advanced Usage (Script Entry Points): Pass app_name to create a new logger with custom handlers. Use set_as_global=True to make it the global logger. For most cases, prefer set_domolibrary_logger() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name
|
str | None
|
Name of the application for logging context (default: None) If provided, creates a NEW logger with handlers. If None, returns existing global logger. |
None
|
debug_color
|
str
|
Color for debug messages (default: cyan) |
'cyan'
|
info_color
|
str
|
Color for info messages (default: green) |
'green'
|
warning_color
|
str
|
Color for warning messages (default: yellow) |
'yellow'
|
error_color
|
str
|
Color for error messages (default: bold_red) |
'bold_red'
|
critical_color
|
str
|
Color for critical messages (default: bold_red) |
'bold_red'
|
env
|
str
|
Environment name ('development', 'production', 'staging', etc.) |
'development'
|
exclude_patterns
|
list[str] | None
|
List of message patterns to filter out (default: None) |
None
|
min_level
|
str | None
|
Minimum log level to display (default: inherit from global logger or INFO) |
None
|
enable_datadog
|
bool | None
|
Enable Datadog handler (auto-detect from env if None) |
None
|
set_as_global
|
bool
|
Set this as cl's global logger (default: False) Only set to True when configuring at entry points. Modules should leave as False. |
False
|
force
|
bool
|
Force setting as global even if user has configured logger (default: False) |
False
|
Returns:
| Type | Description |
|---|---|
ColoredLogger
|
ColoredLogger instance with automatic color application |
Example
Standard usage - get existing global logger (recommended for modules)¶
logger = get_colored_logger() await logger.info("Success!") # Will be green
Advanced usage - create new logger at entry point¶
logger = get_colored_logger( ... app_name="MyApp", ... min_level="DEBUG", ... set_as_global=True, # Set as global for all modules ... )
For entry points, prefer set_domolibrary_logger() instead:¶
from crew_dcs.utils.logging import set_domolibrary_logger logger = set_domolibrary_logger(app_name="MyApp", min_level="DEBUG")
Source code in src/crew_dcs/utils/logging/colored_logger.py
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 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
log_call ¶
log_call(
action_name: str | None = None,
level_name: str | None = None,
config: Any = None,
color: str | None = None,
logger: CrewLogger | None = None,
log_level: str | None = None,
**kwargs: Any
) -> Callable
Compatibility wrapper for log_call decorator.
Maps old API parameters to new log_function_call API. Also filters out 'logger' parameter that log_function_call injects.
Note: Route functions (level_name='route') default to DEBUG level for API operations. Business logic decisions should use INFO level explicitly in method implementations.
Source code in src/crew_dcs/utils/logging/colored_logger.py
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 | |
set_domolibrary_logger ¶
set_domolibrary_logger(
logger: ColoredLogger | None = None,
set_as_global: bool = True,
**kwargs
) -> ColoredLogger
Explicitly set the domolibrary logger, optionally making it the global logger.
This is the recommended way to configure logging for domolibrary if you want colored output throughout the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
ColoredLogger | None
|
Existing ColoredLogger instance, or None to create a new one |
None
|
set_as_global
|
bool
|
Set this as cl's global logger (default: True) |
True
|
**kwargs
|
Arguments to pass to get_colored_logger if creating a new instance |
{}
|
Returns:
| Type | Description |
|---|---|
ColoredLogger
|
The ColoredLogger instance |
Example
Option 1: Use default colored logger¶
from crew_dcs.utils.logging import set_domolibrary_logger logger = set_domolibrary_logger()
Option 2: Create with custom colors¶
logger = set_domolibrary_logger( ... info_color="blue", ... error_color="magenta" ... )
Option 3: Use your own ColoredLogger¶
my_logger = ColoredLogger(base_logger=my_base_logger) set_domolibrary_logger(logger=my_logger)
Source code in src/crew_dcs/utils/logging/colored_logger.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |